001.csv
我有下面要绘制的数据。我需要两个图:一个显示数据,另一个显示对数回归。我可以毫无问题地绘制数据的线(还可以添加线性回归线),但我就是无法在其中得到对数回归线。据我所知,我需要使用gnuplot
它,但不幸的是我无法让它工作。任何帮助都将不胜感激。
干杯,
水泥
%%%%%%%%%%%%%%%%%%%%%%
%This is my data:
\begin{filecontents}{001.csv}
1990 275
1991 187
1992 249
1993 177
1994 212
1995 303
1996 323
1997 364
1998 461
1999 427
2000 683
2001 553
2002 571
2003 682
2004 585
2005 608
2006 609
2007 643
2008 719
2009 545
\end{filecontents}
%%%%%%%%%%%%%%%%%%%%%%
%This is my plot for the data:
\begin{tikzpicture}
\begin{axis}[
axis on top=false, axis x line=middle, axis y line=middle,
xlabel={\footnotesize Year},
ylabel={\footnotesize Frequency count},
xmin=1975, xmax=2009,
ymin=0, ymax=900,
xtick={1980,1985,1990,1995,2000,2005,2009},
ytick={0,100,200,300,400,500,600,700,800},
legend pos=north east,
legend style={font=\fontsize{6}{5}\selectfont},
ymajorgrids=true,
xmajorgrids=true,
grid style=dashed,
/pgf/number format/.cd,
1000 sep={}
]
\addplot[
smooth,tension=0.9,color=blue,mark=diamond
] table {001.csv};
%Somewhere here I need a logarithmic regression line.
\end{axis}
\end{tikzpicture}
%%%%%%%%%%%%%%%%%%%%%%
答案1
正如我在评论中所说,数据点看起来不像对数图是一个好的选择,或者说,比线性图更好。所以在这里我提出了一个显示两条线的解决方案。
\begin{filecontents}{001.csv}
1990 275
1991 187
1992 249
1993 177
1994 212
1995 303
1996 323
1997 364
1998 461
1999 427
2000 683
2001 553
2002 571
2003 682
2004 585
2005 608
2006 609
2007 643
2008 719
2009 545
\end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
xlabel={\footnotesize Year},
ylabel={\footnotesize Frequency count},
xmin=1985, xmax=2014,
ymin=0, ymax=890,
ymajorgrids=true,
xmajorgrids=true,
grid style=dashed,
/pgf/number format/.cd,
1000 sep={},
]
\addplot[
only marks,
color=blue,
mark=diamond*,
] table {001.csv};
\addplot [draw=red,very thick] gnuplot [raw gnuplot] {
f(x)= a*log(x) + b;
fit f(x) "001.csv" using 1:2 via a,b;
plot [x=1990:2009] f(x);
};
\addplot [draw=green,very thick, dashed] gnuplot [raw gnuplot] {
% give some useful initial values
a=25;
b=-50000;
f(x)= a*x + b;
fit f(x) "001.csv" using 1:2 via a,b;
plot [x=1990:2009] f(x);
};
\end{axis}
\end{tikzpicture}
\end{document}