1

I am trying to filter some data before plotting it and showing a regression line. I am basing my approach on https://tex.stackexchange.com/a/69173/110494 . I want to filter the same data file in different ways. The mock-up below filters based on the value of column c. It plots the data how I want, but as soon as I uncomment the commented out code in the preamble in preparation to draw a second plot (also commented out) I get this error:

! Package pgfplots Error: \pgfplotslistfront\ from \pgfplotstable@colnames\ although list is EMPTY.

Can anyone explain this, or provide a solution?

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable,ifthen}
\begin{filecontents}{data.csv}
a, b, c, d, e
0, 1, 1, 8, 13
0, 1, 1, 4, 15
0, 1, 1, 4, 14
0, 1, 1, 5, 12
0, 1, 1, 6, 15
0, 1, 1, 7, 14
0, 1, 2, 10, 21
0, 1, 2, 12, 23
0, 1, 2, 14, 25
0, 1, 2, 16, 28
\end{filecontents}
\newcommand{\pgfplotstablefilterrows}[3]
{ \pgfplotstablegetrowsof{#1}
  \pgfmathsetmacro{\NumOfRows}{\pgfplotsretval}
  \pgfmathsetmacro{\MaxRow}{\NumOfRows-1}
  \pgfplotstablegetcolsof{#1}
  \pgfmathsetmacro{\NumOfCols}{\pgfplotsretval}
  \pgfplotstabletranspose{\TransposedData}{#1}
  \pgfplotstableset{create on use/TransposedHead/.style={copy column from table={\TransposedData}{[index]0}}}
  \pgfplotstablenew[columns={TransposedHead}]{\NumOfCols}{\TransposedFilteredData}
  \pgfplotsforeachungrouped \pgfplotstablerowindex in {0,1,...,\MaxRow}{ % Row loop
    #3
  }
  \pgfplotstabletranspose[colnames from=TransposedHead,input colnames to=]{#2}{\TransposedFilteredData}
  \pgfplotstableclear{\TransposedData}
  \pgfplotstableclear{\TransposedFilteredData}
}
\pgfplotstableread[col sep=comma]{data.csv}{\dataone}
\pgfplotstablefilterrows{\dataone}{\FilteredDataOne}
  {\pgfplotstablegetelem{\pgfplotstablerowindex}{[index]2}\of\dataone\ifthenelse{\equal{\pgfplotsretval}{1}}
  {\pgfplotstablecreatecol[copy column from table={\TransposedData}{\pgfplotstablerowindex}]{\pgfplotstablerowindex}{\TransposedFilteredData}}
  {}
}
% uncommenting this gives an error
% \pgfplotstableread[col sep=comma]{data.csv}{\datatwo}
% \pgfplotstablefilterrows{\datatwo}{\FilteredDataTwo}
%   {\pgfplotstablegetelem{\pgfplotstablerowindex}{[index]2}\of\datatwo\ifthenelse{\equal{\pgfplotsretval}{2}}
%   {\pgfplotstablecreatecol[copy column from table={\TransposedData}{\pgfplotstablerowindex}]{\pgfplotstablerowindex}{\TransposedFilteredData}}
%   {}
% }
\begin{document}
\begin{tikzpicture}
  \begin{axis}[unbounded coords=jump]
    \addplot [only marks] table [x=d, y=e] {\FilteredDataOne};
    \addplot [smooth,no marks, thick, red] table [x=d, y={create col/linear regression={y=e}}] {\FilteredDataOne};
  \end{axis}
\end{tikzpicture}
% \begin{tikzpicture}
%   \begin{axis}[unbounded coords=jump]
%     \addplot [only marks] table [x=d, y=e] {\FilteredDataTwo};
%     \addplot [smooth,no marks, thick, red] table [x=d, y={create col/linear regression={y=e}}] {\FilteredDataTwo};
%   \end{axis}
% \end{tikzpicture}
\end{document}
1

Can't say why, but it works if you do not clear the two transposed tables, i.e. comment out/remove

\pgfplotstableclear{\TransposedData}
\pgfplotstableclear{\TransposedFilteredData}

from the definition of \pgfplotstablefilterrows.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable,ifthen}
\begin{filecontents}{data.csv}
a, b, c, d, e
0, 1, 1, 8, 13
0, 1, 1, 4, 15
0, 1, 1, 4, 14
0, 1, 1, 5, 12
0, 1, 1, 6, 15
0, 1, 1, 7, 14
0, 1, 2, 10, 21
0, 1, 2, 12, 23
0, 1, 2, 14, 25
0, 1, 2, 16, 28
\end{filecontents}
\newcommand{\pgfplotstablefilterrows}[3]
{ \pgfplotstablegetrowsof{#1}
  \pgfmathsetmacro{\NumOfRows}{\pgfplotsretval}
  \pgfmathsetmacro{\MaxRow}{\NumOfRows-1}
  \pgfplotstablegetcolsof{#1}
  \pgfmathsetmacro{\NumOfCols}{\pgfplotsretval}
  \pgfplotstabletranspose{\TransposedData}{#1}
  \pgfplotstableset{create on use/TransposedHead/.style={copy column from table={\TransposedData}{[index]0}}}
  \pgfplotstablenew[columns={TransposedHead}]{\NumOfCols}{\TransposedFilteredData}
  \pgfplotsforeachungrouped \pgfplotstablerowindex in {0,1,...,\MaxRow}{ % Row loop
    #3
  }
  \pgfplotstabletranspose[colnames from=TransposedHead,input colnames to=]{#2}{\TransposedFilteredData}
%  \pgfplotstableclear{\TransposedData}
%  \pgfplotstableclear{\TransposedFilteredData}
}

\pgfplotstableread[col sep=comma]{data.csv}{\dataone}
\pgfplotstablefilterrows{\dataone}{\FilteredDataOne}
{
  \pgfplotstablegetelem{\pgfplotstablerowindex}{[index]2}\of\dataone
    \ifthenelse{\equal{\pgfplotsretval}{1}}
     {\pgfplotstablecreatecol[copy column from table={\TransposedData}{\pgfplotstablerowindex}]{\pgfplotstablerowindex}{\TransposedFilteredData}}
     {}
}

% uncommenting this gives an error
\pgfplotstableread[col sep=comma]{data.csv}{\datatwo}
\pgfplotstablefilterrows{\datatwo}{\FilteredDataTwo}
{
  \pgfplotstablegetelem{\pgfplotstablerowindex}{[index]2}\of\datatwo
    \ifthenelse{\equal{\pgfplotsretval}{2}}
     {\pgfplotstablecreatecol[copy column from table={\TransposedData}{\pgfplotstablerowindex}]{\pgfplotstablerowindex}{\TransposedFilteredData}}
     {}
}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[unbounded coords=jump]
    \addplot [only marks] table [x=d, y=e] {\FilteredDataOne};
    \addplot [smooth,no marks, thick, red] table [x=d, y={create col/linear regression={y=e}}] {\FilteredDataOne};
  \end{axis}
\end{tikzpicture}
 \begin{tikzpicture}
   \begin{axis}[unbounded coords=jump]
     \addplot [only marks] table [x=d, y=e] {\FilteredDataTwo};
     \addplot [smooth,no marks, thick, red] table [x=d, y={create col/linear regression={y=e}}] {\FilteredDataTwo};
   \end{axis}
 \end{tikzpicture}
\end{document}
0

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.