使用对数回归绘制表格数据的趋势图

使用对数回归绘制表格数据的趋势图

给定图表中的几个数据点,即第一个卤素的沸点,我需要“预测”砹的沸点。有人告诉我,预测不必非常精确,几乎任何估计都是可以接受的。我希望我的预测尽可能准确,并制定了一个使用对数回归的计划。

左侧为原始图表。右侧为我的预测。难点在于如何将对数趋势一直延伸到砹 BP。

我在这里卡住了。我的第二个问题是,为什么我的第二张图没有像第一张图那样的蓝点?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,angles,quotes,intersections,decorations.pathmorphing,positioning,matrix,fit}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage[version=4]{mhchem}
\usepackage{filecontents}
\begin{document}
\begin{filecontents}{bpdata.txt}
{Electrons} {Boiling Point}
18 -188
34 -34
70 59
106 184
\end{filecontents}
\begin{tikzpicture}[font=\tiny,scale=1]
\begin{axis}[
grid=both,
grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!50},
minor tick num=4,
axis x line=center,
axis y line=center,
axis line style={-triangle 45,shorten >=-10pt, shorten <=-10pt},
ymin=-200,
ymax=200,
xmin=0,
xmax=120,
]
\addplot table {bpdata.txt};

\node at (axis cs:28,-188) {\ce{F2}};
\node at (axis cs:44,-44) {\ce{Cl2}};
\node at (axis cs:80,55) {\ce{Br2}};
\node at (axis cs:116,184) {\ce{I2}};

\end{axis}
\end{tikzpicture}
\begin{filecontents}{bpastatine.txt}
{Electrons} {Boiling Point}
18 -188
34 -34
70 59
106 184
170 337
\end{filecontents}
\begin{tikzpicture}[font=\tiny,scale=1]
\begin{axis}[
grid=both,
grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!50},
minor tick num=4,
axis x line=center,
axis y line=center,
axis line style={-triangle 45,shorten >=-10pt, shorten <=-10pt},
ymin=-200,
ymax=400,
xmin=0,
xmax=200,
]
\addplot[blue] table {bpdata.txt};

\addplot[red,raw gnuplot,smooth,dashed,domain={10,170}] gnuplot {
f(x)=a*log(x)+b;
fit f(x) 'bpastatine.txt' using 1:2 via a,b;
plot [x=0:85] f(x);
};

\node at (axis cs:28,-188) {\ce{F2}};
\node at (axis cs:44,-44) {\ce{Cl2}};
\node at (axis cs:80,55) {\ce{Br2}};
\node at (axis cs:116,184) {\ce{I2}};
\node at (axis cs:178,337) {\ce{At2}};
\end{axis}
\end{tikzpicture}
\end{document}

答案1

你只犯了一个小错误。

请查看代码中的注释以了解详细信息。

% used PGFPlots v1.14
    \begin{filecontents}{bpdata.txt}
        {Electrons} {Boiling Point}
        18 -188
        34 -34
        70 59
        106 184
    \end{filecontents}
    \begin{filecontents}{bpastatine.txt}
        {Electrons} {Boiling Point}
        18 -188
        34 -34
        70 59
        106 184
        170 337
    \end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
%\usepackage{pgfplotstable}
    \usetikzlibrary{
        % you should use the new library instead of just `arrows'
        arrows.meta,
    }
    \pgfplotsset{
        % use this `compat' level or higher so you don't need to prepend
        % every coordinate with `axis cs:'
        compat=1.11,
    }
\usepackage[version=4]{mhchem}
\begin{document}
    \begin{tikzpicture}[
        font=\tiny,
    ]
        \begin{axis}[
            grid=both,
            grid style={
                line width=.1pt,
                draw=gray!10,
            },
            major grid style={
                line width=.2pt,
                draw=gray!50,
            },
            minor tick num=4,
            axis x line=center,
            axis y line=center,
            axis line style={
                % that is the corresponding new name for `triangle 45'
                -Triangle[],
                shorten >=-10pt,
                shorten <=-10pt,
            },
            ymin=-200,
            ymax=400,
            xmin=0,
            xmax=200,
        ]
            % by adding the `+' sign all the default attributes from the
            % -- in this case default -- `cycle list' are used and the options
            % given are *appended* to these
            % (otherwise the default style not coming from a `cycle list' is
            %  used and the additional options are used)
            % because we have "measured" data here, I prefer not showing a line
            \addplot+ [only marks] table {bpdata.txt};

            \addplot [
                red,
                smooth,
                dashed,
                raw gnuplot,
%                % this doesn't do anything in `raw gnuplot'
%                % (besides that you used the wrong syntax. The separator is a
%                %  colon, so `domain=10:170' would have been right)
%                domain={10,170},
            ] gnuplot {
                f(x)=a*log(x)+b;
                fit f(x) 'bpastatine.txt' using 1:2 via a,b;
                % -------------------------------------------------------------
                % this is the part to answer your question
                % -------------------------------------------------------------
                % here you have to give the domain which should be plotted
                % I increased the end value to the value of the below given label
                % (and I increased the start value to 10. Change it back to 0
                %  to see why.)
                plot [x=10:178] f(x);
                % ------------------------------------------------------------------
            };

            % as stated above: now `axis cs' isn't needed any more
            \node at (28,-188) {\ce{F2}};
            \node at (44,-44)  {\ce{Cl2}};
            \node at (80,55)   {\ce{Br2}};
            \node at (116,184) {\ce{I2}};
            \node at (178,337) {\ce{At2}};
        \end{axis}
    \end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容