使用 pgfplots 在 tikzpicture 中绘制方程式不起作用

使用 pgfplots 在 tikzpicture 中绘制方程式不起作用

我想使用 pgfplots 包在 lateX 中绘制这个方程式:0.0172x + 2.9971。我已经制作了一个轴,并使用数据文件中的数据点绘制了另一个图形。数据点和相应的拟合在坐标系中绘制,但没有绘制方程式。非常感谢您的帮助!!

我使用了这段代码:

\begin{figure}[h]

\caption{Plot of different electric potentials measured at the different PH's of the calibration solutions. A linear fit for all points is shown in blue. The green line is a fit for PH range 5,8-6,8 and the red line a fit for PH range 6,8-7,8}

\begin{flushleft}

\begin{tikzpicture}[] 

    \begin{axis}[
    title={\large{Electric potential at different PH's for other groups}},
    height={100 mm},
    width={150 mm},
    xlabel={PH [Without unit]},
    ylabel={Electric potential [mV]},
    xmin=0, xmax=14,
    ymin=50, ymax=300,
    xtick={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14},
    ytick={0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270},
    legend pos=north west,
    ymajorgrids=true,
    xmajorgrids=true,
    grid style=dashed,
    ]

\addplot[only marks] file {PHMeasuredata.txt};

\addplot + gnuplot [raw gnuplot, id=AlternativePlot, mark=smooth, draw=red]{
    f(x) = a*x+b;
    a=20;
    b=30;
    fit f(x) "AlternativePlot.txt" using 1:2 via a, b;
    plot [x=0:14] f(x);};

\addplot[no markers, blue]{0.0172*x + 2.9971};

\end{axis}
\end{tikzpicture}
\end{flushleft}
\end{figure}

答案1

问题在于您没有考虑表达式将给出什么样的值-0.0172*x + 2.9971它们将介于 2.9971 和 3.24 之间。但您已设置,这意味着该线远远超出了您要绘制的区域。x[0,14]ymin=50

我当然没有你的数据文件,但是如果我删除那些图以及轴中的和ymin设置,我会得到如下结果:ymaxytick

在此处输入图片描述

它停止在 5 的原因是默认域是-5:5,所以您想要添加domain=0:14或类似于\addplot选项。

其他一些注意事项:

  • \large不是接受参数的命令,因此应将其用作{\large text}not \large{text}

  • 我不知道您的设置,但至少默认情况下article15 厘米对于文本块来说太宽了。\textwidth不过您可以将宽度设置为。

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{figure}[h]

\caption{Plot of different electric potentials measured at the different PH's of the calibration solutions. A linear fit for all points is shown in blue. The green line is a fit for PH range 5,8-6,8 and the red line a fit for PH range 6,8-7,8}

\begin{tikzpicture}
    \begin{axis}[
    title={\large Electric potential at different PH's for other groups},
    height={80 mm},
    width={\textwidth},
    xlabel={PH [Without unit]},
    ylabel={Electric potential [mV]},
    xmin=0, xmax=14,
%    ymin=50, ymax=300,
    xtick={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14},
%    ytick={0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270},
    legend pos=north west,
    ymajorgrids=true,
    xmajorgrids=true,
    grid style=dashed,
    ]

%\addplot[only marks] file {PHMeasuredata.txt};

%\addplot + gnuplot [raw gnuplot, id=AlternativePlot, mark=smooth, draw=red]{
%    f(x) = a*x+b;
%    a=20;
%    b=30;
%    fit f(x) "AlternativePlot.txt" using 1:2 via a, b;
 %   plot [x=0:14] f(x);};

\addplot[blue,domain=0:14]{0.0172*x + 2.9971};

\end{axis}
\end{tikzpicture}
\end{figure} 
\end{document}

相关内容