pgfplots:空列表错误

pgfplots:空列表错误

我试图在绘制数据并显示回归线之前过滤一些数据。我的方法基于 https://tex.stackexchange.com/a/69173/110494。我想以不同的方式过滤同一个数据文件。下面的模型根据 c 列的值进行过滤。它按照我想要的方式绘制数据,但是当我取消注释序言中注释掉的代码以准备绘制第二个图(也注释掉)时,我收到此错误:

!程序包 pgfplots 错误:\pgfplotslistfront\ 来自 \pgfplotstable@colnames\ 尽管列表为空。

有人可以解释这一点,或者提供解决方案吗?

\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

说不出为什么,但如果你这样做,它就会起作用不是清除两个转置表,即注释掉/删除

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

根据 的定义\pgfplotstablefilterrows

在此处输入图片描述

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

相关内容