我正在尝试绘制一些我在 pgfplots 中应用了线性回归的数据点的图。但是,我对使用所有数据点进行回归不感兴趣。我如何忽略前 3 行?
我已经尝试过该设置skip first n=2
,但似乎没有任何效果,因为它在所有点上运行回归。
\documentclass[11pt,a4paper]{scrartcl}
\usepackage[latin1]{inputenc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\begin{document}
\pgfplotstableread[col sep=comma]{%
a,b
1,-12.5894348439729
2,-12.7542953929274
3,-12.8646539338807
4,-13.2133933292766
5,-13.7892202984399
6,-14.5668071605761
}\test
\begin{tikzpicture}
\begin{axis}[%
width=\textwidth,
axis x line=bottom,
axis y line=left]
\addplot[only marks, blue] table[x={a}, y={b}]{\test};
\addplot[mark=none, blue] table[skip first n=2, x={a}, y={create col/linear regression={y=b}}]{\test};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
也许这有点像暴力破解方法,但你可以使用\pgfplotstablesave[skip rows between index={0}{2}]{\test}{test1.dat}
让我们使用更好的,这意味着更清晰的(!)MWE:
\documentclass[border=5pt, varwidth]{standalone}
%\documentclass[11pt,a4paper]{scrartcl}
\usepackage[latin1]{inputenc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\begin{document}
\pgfplotstableread[col sep=comma]{%
a,b
1,1
2,2
3,3
4,4
5,5
6,6
}\test
\pgfplotstabletypeset[]{\test} \hspace{1cm}
%
\pgfplotstabletypeset[
%skip first n=2, % Does not work
skip rows between index={0}{2} % Works
]{\test} \hspace{3cm}
%
% Save as costumized table
\pgfplotstablesave[skip rows between index={0}{2}]{\test}{test1.dat}
% Test:
\pgfplotstabletypeset[]{test1.dat}
\begin{tikzpicture}
\begin{axis}[
width=\textwidth,
axis x line=bottom,
axis y line=left,
xlabel=a, ylabel=b,
xtick=data
]
\addplot[mark=*] table[x={a}, y={b}]{\test}; % Original
\addplot[mark=*, blue] table[
skip first n=2, % Does not work
skip rows between index={0}{2}, % Does not work
x={a}, y expr={\thisrow{b}+1}
%y={create col/linear regression={y=b}}
]{\test};
\addplot[mark=*, red] table[
%skip first n=2, % Does not work
%skip rows between index={0}{2}, % Does not work
x={a}, y expr={\thisrow{b}+2}
]{test1.dat};
\end{axis}
\end{tikzpicture}
\end{document}