I want to create a plot using a non-standard plot-style that I don't see in the pgfplots manual. It would be nice if I could take advantage of the \pgfplotstableread function and then somehow loop over the rows and columns of the table to create my custom plot with a tikz picture. I have been looking at the pgfplots and pgfplotstable manuals and I am having trouble seeing if there is a way to do this! Any help would be appreciated!

  • 1
    You might want to take a look at the question about Kiviat plots, there's a couple of ideas there that could help you. Azoun used pgfplotstable directly, I let pgfplots take care of handling the looping and only changed the way the points were positioned using mathematical expressions. If you let us know more exactly what you're trying to do, we might be able to help more.
    – Jake
    Mar 8 '11 at 19:05

Thanks to Jake's suggestion (and a more thorough read of the pgfplotstable manual) I found the \pgfplotstablegetelem command. This was exactly what I needed to make my plot. Here is an example of what I wanted my plot to look like on the following fake data

0.2 0.3 0.6 0.3
0.3 0.8 0.9 0.4
0.4 0.6 0.7 0.2
0.5 0.7 0.95 0.8
0.1 0.2 0.3 0.4
0.3 0.5 0.4 0.6

I set up this table in pairs of rows. The even rows give the lower bound of a range in my plot and the odd rows give the upper bound in the range.




\pgfplotstablegetrowsof{\datawhead} %Determine no. of rows
\pgfplotstablegetcolsof{\data} % Determine no. of cols

\draw[->,ultra thick] (0,0)--(1,0); % axes
\draw[->,ultra thick] (0,0)--(0,1);

\pgfmathsetmacro{\r}{\rows-2} % do some math to separate the range plots and categories
\pgfmathsetmacro{\seps}{1/ \lines*0.8}

\foreach \j in {0,2,...,\r}{ % category loop
    \foreach \i/\clr in {0/red,1/blue,2/green,3/blue!40!red}{ %subcategory loop
        \pgfmathsetmacro{\x}{\bufs + \j*\catlen+\i*\seps} % x coordinate
        \pgfmathsetmacro{\ya}{\pgfplotsretval} % y coord 1
        \pgfmathsetmacro{\yb}{\pgfplotsretval} % y coord 2
        \draw[lightgray, thin](\x,0)--(\x,1);
        \node[rectangle,fill=\clr,inner sep=1.2pt,minimum width=6pt](bottom) at (\x,\ya){};
        \node[rectangle,fill=\clr,inner sep=1.2pt,minimum width=6pt](top) at (\x,\yb){};

    \node[anchor=west] at (\bufs+\j*\catlen,-0.1){Category \jm};


The output is here. This is almost exactly what I wanted and I though the code is a bit messy, it allows me to change my data and generate an updated plot easily. One last thing though... I can't seem to be able to get my categories indices to be integers.

Thank you Jake and also thanks to the authors of pgfplotstable!

The final figure

  • 1
    pgfmath has an int command that truncates the decimal part of a number. However, \pgfmathsetmacro will always save its result as a floating point number, so you will have to use the slightly more verbose \pgfmathparse{int(\j/2)}\let\jm=\pgfmathresult to get integer category numbers.
    – Jake
    Mar 10 '11 at 9:18
  • @Jake - I think it will also be possible to use \pgfmathtruncatemacro as an alternative, to obtain an integer.
    – sdaau
    May 28 '14 at 22:45
  • You can also use siunitx to round numbers to appropriate numbers of digits. See this question.
    – Jost
    Mar 13 '15 at 15:01

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.