我尝试绘制以下数据:
\pgfplotstableread[col sep=semicolon]{
record;value
2004;48
2009;34
2010;32
2016;22
2020;21
}\valuetotest
经过
\pgfkeys{
/pgf/number format/.cd,
set decimal separator={,{\!}},
set thousands separator={}}
\begin{tikzpicture}[]
\begin{axis}[
grid=both, %none
major grid style={thick},
xtick distance={5},
ytick distance={10},
minor x tick num=4,
minor y tick num=4,
width=13.5cm, height=13cm,
axis x line=middle,
axis y line=middle,
samples=100,
ymin=-0.2, ymax=60.5,
xmin=2000, xmax=2025,
domain=2000:2025,
xlabel={Years},
ylabel={$\mu g/m^3$}
]
\addplot[only marks,mark=square*,blue] table[x=record,y=value] {\valuetotest};
\addlegendentry{values}
\addplot[no marks,blue,dashed,] table[header=false,x=record,y={create col/linear regression={y=value}}] {\valuetotest};
\addlegendentry{pgfplots computed lin regr}
\addplot[smooth,orange,domain=2000:2025]{\pgfplotstableregressiona*x+\pgfplotstableregressionb};
\addlegendentry{a =\pgfmathprintnumber[]{\pgfplotstableregressiona}; b=\pgfmathprintnumber[]{\pgfplotstableregressionb}}
\addplot[smooth,blue,domain=2000:2025]{-1.674744898*x+3400.651786};
\addlegendentry{manual lin regr}
\end{axis}
\end{tikzpicture}
但如图所示,pgfplots 似乎计算出了错误的线性回归值(我用 excel 计算了“手动值”)。
提前感谢