3

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
    you can skip lines while reading the table not whileusing it – percusse May 1 '18 at 10:29
  • So I have to read the file twice? That's annoying. – Nicky Mattsson May 1 '18 at 10:40
  • 1
    skip lines n is for skipping file headers not data filtering. – percusse May 1 '18 at 10:42
  • According to this, I believe it should be possible. tex.stackexchange.com/questions/247808/… – Nicky Mattsson May 1 '18 at 10:43
  • 4
    That's reading the same file multiple times by creating \jobname.dat file – percusse May 1 '18 at 10:49
1

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.