1

I'm trying to achieve something a little ambitious and I'm stuck with how to manipulate table data with pgfplots and pgfplotstable.

I have a data file with benchmark results of a number of different tools among a certain number of different data sets. The file has this form:

datasets tool1 tool2 tool3 tool4 tool5 tool6
dataset1     1     2     3     4     5     6
dataset2     1     2     3     4     5     6
dataset3     1     2     3     4     5     6
dataset4     1     2     3     4     5     6
dataset5     1     2     3     4     5     6
dataset6     1     2     3     4     5     6
dataset7     1     2     3     4     5     6
dataset8     1     2     3     4     5     6

that is, the tools are placed on different columns, and the data sets on different rows. I would like to plot this data in a sort of table of the same shape, with a bar plot for each row, showing the timings of different tools on the dataset of each row.

Thus, the first column of the table should report the names of the datasets, and the second column should contain the bar plots.

The problems I have:

  • How to iterate over the rows of the file to produce the table?
    • I can imagine iterating with \foreach, but I have problems in obtaining a bar plot from a single given row of the file.
  • How to generate a single bar plot from each single row?

I don't know how to mock my intended result. It is something like this example, with bar plots instead of those rain plots, and without hardcoding the names of the rows on the first column.

Any suggestion?

3
  • You should look at datatool and csvsimple. Related: tex.stackexchange.com/questions/83888/…
    – user30471
    Apr 27, 2017 at 10:32
  • The question does not seem to be related at all. I don't have problems plotting simple coordinates from a file. Also, I'd prefer to stay with pgfplots if possible. Apr 27, 2017 at 10:54
  • The main point here is being able to use a single line at a time to make multiple bar plots. Apr 27, 2017 at 10:59

1 Answer 1

3

If your file would is named eg. mydata.csv, you can use

\pgfplotstabletranspose[colnames from=datasets,input colnames to=datasets]{\data}{mydata.csv}

Example 1:

\begin{filecontents*}{mydata.csv}
datasets tool1 tool2 tool3 tool4 tool5 tool6
dataset1     2     3     4     5     6     7
dataset2     1     2     3     4     5     6
dataset3     1     2     3     4     5     6
dataset4     1     2     3     4     5     6
dataset5     1     2     3     4     5     6
dataset6     1     2     3     4     5     6
dataset7     1     2     3     4     5     6
dataset8     1     2     3     4     5     6
\end{filecontents*}

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.14}
\begin{document}
\pgfplotstabletranspose[colnames from=datasets,input colnames to=datasets]{\data}{mydata.csv}
  \foreach \dataset in {1,...,8}{%
    \noindent Dataset \dataset\qquad
    \begin{tikzpicture}[baseline={([yshift=-\baselineskip]p.north)}]
      \begin{axis}[
        height=4cm,
        width=\axisdefaultwidth,
        ybar,
        xtick=data,
        xticklabels from table={\data}{datasets},
        name=p
      ]
          \addplot table[x expr=\coordindex,y index=\dataset]{\data};
      \end{axis}
    \end{tikzpicture}%
    \par
  }%
\end{document}

enter image description here


Example 2:

\begin{filecontents*}{mydata.csv}
datasets tool1 tool2 tool3 tool4 tool5 tool6
dataset1     2     3     4     5     6     7
dataset2     1     2     3     4     5     6
dataset3     1     2     3     4     5     6
dataset4     1     2     3     4     5     6
dataset5     1     2     3     4     5     6
dataset6     1     2     3     4     5     6
dataset7     1     2     3     4     5     6
dataset8     1     2     3     4     5     6
\end{filecontents*}
\documentclass{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
  \pgfplotstabletranspose[colnames from=datasets,input colnames to=datasets]{\data}{mydata.csv}
  \begin{axis}[
    width=\textwidth,
    ybar,
    bar width=2pt,
    xtick=data,
    xticklabels from table={\data}{datasets},
    legend pos=north west,
    cycle list name=color list,
    every axis plot/.append style=fill
  ]
  \pgfplotsinvokeforeach {1,...,8}
    {
      \addplot table[x expr=\coordindex,y index=#1]{\data};
      \addlegendentry{dataset#1}
    }
  \end{axis}
\end{tikzpicture}
\end{document}

Result:

enter image description here

5
  • Thank you, but this is not exactly the kind of plot I need. I have more than 8 datasets, something like 30 datasets, so they need to be listed vertically. The idea is to typeset a table with the name of the dataset on the first column and a bar plot for each row, showing different tools side-by-side. Is it possible? Apr 27, 2017 at 12:55
  • See my updated answer.
    – esdd
    Apr 27, 2017 at 13:16
  • That's the starting point I needed, thank you! Apr 27, 2017 at 13:23
  • I'm trying to make each iteration part of a row of a tabular but I get a lot of strange error messages (undefined control sequence). It seems the problem is the \\ command. Any hint? Apr 27, 2017 at 18:09
  • @esdd I have been trying to something similar, but I am struggling so hard, would you be able to help me please?
    – 3kstc
    May 9, 2018 at 4:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .