# How to partition a pgfplotstable to use in multiple tikzpictures? (and duplicate a row)

I'm trying to create multiple plots from one pgfplotstable (\data below). I can create the plots by manually editing the data (\dataA and \dataB below) but this is error-prone. Essentially, I need help:

• partitioning the table based on column cat
• duplicating the last row (except xmin is xmax from the previous row)
• creating a loop to create multiple tikzpictures [this I can probably figure out myself!]

\documentclass{article}
\usepackage{pgfplotstable}

\pgfplotsset{compat=1.15}

\begin{document}
x   xmin xmax par1    y cat par2
0.5    0    1  yes  0.3   1   no
1.5    1    2  yes  0.6   1   no
2.5    2    3  yes  0.7   1   no
0.5    0    1  yes  0.4   2   no
1.5    1    2  yes  0.5   2   no
2.5    2    3  yes  0.9   2   no
}\data

x   xmin xmax par1    y cat par2
0.5    0    1  yes  0.3   1   no
1.5    1    2  yes  0.6   1   no
2.5    2    3  yes  0.7   1   no
2.5    3    3  yes  0.7   1   no
}\dataA

x   xmin xmax par1    y cat par2
0.5    0    1  yes  0.4   2   no
1.5    1    2  yes  0.5   2   no
2.5    2    3  yes  0.9   2   no
2.5    3    3  yes  0.9   2   no
}\dataB

\begin{tikzpicture}
\begin{axis}[ybar, ymin=0, ymax=1, xmin=-0.5, xmax=3.5]
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[ybar, ymin=0, ymax=1, xmin=-0.5, xmax=3.5]
\end{axis}
\end{tikzpicture}

\end{document}


Regarding partitioning the data, I have been able to print the partitioned table, but then how can I use it as input for \addplot?

\newcommand{\filtertable}[2]{
\pgfplotstabletypeset[
columns/par1/.style={string type},
columns/par2/.style={string type},
row predicate/.code={%
\pgfplotstablegetelem{##1}{cat}\of{#1}
\ifnum\pgfplotsretval=#2\relax
\else\pgfplotstableuserowfalse\fi}
]{#1}
}

\filtertable{\data}{1}
\filtertable{\data}{2}


Regarding duplicating the last row, the only relevant value in the last rows is xmin. According to the PGF manual with ybar interval "The last y value will be ignored." I have unsuccessfully tried to create the same plot without the additional row. Note the distance between xmin and xmax must not always equal 1.

One way is to install a filter like

unbounded coords=discard, x filter/.expression={\thisrow{cat}==1 ? x : nan}

MWE:

\documentclass{article}
\usepackage{pgfplotstable}

\pgfplotsset{compat=1.15}

\begin{document}
x   xmin xmax par1    y cat par2
0.5    0    1  yes  0.3   1   no
1.5    1    2  yes  0.6   1   no
2.5    2    3  yes  0.7   1   no
0.5    0    1  yes  0.4   2   no
1.5    1    2  yes  0.5   2   no
2.5    2    3  yes  0.9   2   no
}\data

x   xmin xmax par1    y cat par2
0.5    0    1  yes  0.3   1   no
1.5    1    2  yes  0.6   1   no
2.5    2    3  yes  0.7   1   no
2.5    3    3  yes  0.7   1   no
}\dataA

x   xmin xmax par1    y cat par2
0.5    0    1  yes  0.4   2   no
1.5    1    2  yes  0.5   2   no
2.5    2    3  yes  0.9   2   no
2.5    3    3  yes  0.9   2   no
}\dataB

\begin{tikzpicture}
\begin{axis}[ybar, ymin=0, ymax=1, xmin=-0.5, xmax=3.5]
x filter/.expression={\thisrow{cat}==1 ? x : nan}] table[x=xmin,y=y] {\data}\closedcycle;
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[ybar, ymin=0, ymax=1, xmin=-0.5, xmax=3.5]

• The error is Unknown function thisrow_unavailable_load_table_directly. Apparently, \thisrow needs to be followed with the actual data table and not the variable \data. – Frank Seifert Apr 30 at 9:20