1

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!]

enter image description here

\documentclass{article}
\usepackage{pgfplotstable}

\pgfplotsset{compat=1.15}

\begin{document}
\pgfplotstableread{
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

\pgfplotstableread{
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

\pgfplotstableread{
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]
    \addplot[black, ybar interval] table[x=xmin,y=y] {\dataA}\closedcycle;
  \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{axis}[ybar, ymin=0, ymax=1, xmin=-0.5, xmax=3.5]
    \addplot[black, ybar interval] table[x=xmin,y=y] {\dataB}\closedcycle;
  \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.

u17
  • 4,976

1 Answers1

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

\pgfplotstableread{
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

\pgfplotstableread{
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]
    \addplot[black, ybar interval,unbounded coords=discard,
    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]
    \addplot[black, ybar interval,unbounded coords=discard,
    x filter/.expression={\thisrow{cat}==2 ? x : nan}] table[x=xmin,y=y] {\data}\closedcycle;
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

  • Thanks for your reply. It doesn't compile on my installation. – u17 Apr 30 '19 at 08:50
  • 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. – u17 Apr 30 '19 at 09:20
  • 1
    @FrankSeifert On my installation it does compile. And since you edited it, you are probably aware of this post. However, on my machine I do not need any of this. –  Apr 30 '19 at 10:44