2

I am trying to produce figure with 2 boxplot prepared plots from an inline data file. I grabbed this code from here to use as a starting point. Given that the wrapper code allows the boxplot data to be read, my data has 5 more lines and the project will have ~20. Currently, the data is read and an addplot call made for each line of data. What I tried to do was call addplot from a loop. \foreach \i in {0,1} for each row (row=1 -> row=\i) did not work, nor did \pgfplotsinvokeforeach{0,1} (row=#1) work, the row not being resolved in both cases. My code that works:

\documentclass[crop=true]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.10}  %was 1.8
\usepgfplotslibrary{statistics}
\makeatletter
\pgfplotsset{
    boxplot prepared from table/.code={
        \def\tikz@plot@handler{\pgfplotsplothandlerboxplotprepared}%
        \pgfplotsset{
            /pgfplots/boxplot prepared from table/.cd,
            #1,
        }
    },
    /pgfplots/boxplot prepared from table/.cd,
        table/.code={\pgfplotstablecopy{#1}\to\boxplot@datatable},
        row/.initial=0,
        make style readable from table/.style={
            #1/.code={
                \pgfplotstablegetelem{\pgfkeysvalueof{/pgfplots/boxplot prepared from table/row}}{##1}\of\boxplot@datatable
                \pgfplotsset{boxplot/#1/.expand once={\pgfplotsretval}}
            }
        },
        make style readable from table=lower whisker,
        make style readable from table=upper whisker,
        make style readable from table=lower quartile,
        make style readable from table=upper quartile,
        make style readable from table=median,
        make style readable from table=lower notch,
        make style readable from table=upper notch
}
\makeatother


\pgfplotstableread[row sep=crcr]{
    lw     lq     med    uq     uw\\
    15.10  21.75  26.50  30.88  42.10\\
    10.30  21.70  26.90  31.83  45.30\\
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[boxplot/draw direction=x, ymin=0,
             ytick={1,...,5}, 
             yticklabels={2010,2011,2012,2013,2014},
             xmin=0, xmax=50,]
  \addplot+[
  boxplot prepared from table={
    table=\datatable,
    row=0,
    lower whisker=lw,
    upper whisker=uw,
    lower quartile=lq,
    upper quartile=uq,
    median=med
  }, boxplot prepared
  ]
  coordinates {};

\pgfplotstablegetcolsof{\datatable}
\pgfmathsetmacro\numberofrows{\pgfplotsretval-1}

%\foreach \i in {0,1}{ %...,\numberofrows}{
  \addplot+[
  boxplot prepared from table={
    table=\datatable,
    row=1, %row=\i,
    lower whisker=lw,
    upper whisker=uw,
    lower quartile=lq,
    upper quartile=uq,
    median=med
  }, boxplot prepared
  ]
  coordinates {};
%} %foreach

\end{axis}
\end{tikzpicture}
\end{document}

Does anybody know how to wrap a loop around a single addplot boxplot prepared? I don"t see anything in the pgfplots 1.10 manual covering pulling boxplot prepared data from a file, nor in this forum. Also, most everything I looked through were for previous versions of pgfplot (<1.10). Doesn't pgfplots 1.10 support boxplots natively? Hopefully someone can figure this out as it would be very useful! Any help is greatly appreciated. There's got to be an easier way to do this! Best regards, Dave.

2

Working through my implementation:

 \pgfplotsinvokeforeach{0,...,\numberofrows}{

I used row=#1 at first, then changed to row= #1 and it is now fine.

Regards, Dave.

  • Also change \pgfplotstablegetcolsof{\datatable} to \pgfplotstablegetrowsof{\datatable}. – Ramakrishnan Kannan Oct 16 '15 at 14:50

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.