5

I am trying to make a plot of some data points which I have applied linear regression to in pgfplots. However, I am not interested in using all data points for the regression. How do I ignore the first, say 3 rows?

I have tried with the setting skip first n=2, but it does not seem to have any effect, as it runs the regression on all points.

\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 Answers1

2

Maybe this is something like a Brute-Force solution, but you can use
\pgfplotstablesave[skip rows between index={0}{2}]{\test}{test1.dat}

Let's use a better, this means clearer (!) MWE:

enter image description here

\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}
cis
  • 8,073
  • 1
  • 16
  • 45