1

I have referred to the question posted here: Read boxplot prepared values from a table. However, it doesn't work very well with external .csv files.

I have the following Table.csv content:

Product,Min,Lower,Median,Upper,Max
Blue,12.5,16,25,32,35
Green,28,32.2,35.5,37,45
Yellow,6.6,15,25.5,30.2,70
Magenta,10,12.2,14.4,16.6,19.1

I tried the following code:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}
\usepackage{pgfplotstable}

\begin{document}


\begin{tikzpicture}
\begin{axis}[boxplot/draw direction=y,
ytick={1,2,3,4},
yticklabels={Blue,Green,Yellow,Magenta}]
%Blue
\addplot+[
  boxplot prepared from table={
    table=Table.csv,
    row=1,
    lower whisker=Min,
    lower quartile= Lower,
    median= Median,
    upper quartile= Upper,
    upper whisker= Max,
  }, boxplot prepared
  ]
  coordinates {};
%Green
\addplot+[
 boxplot prepared from table={
    table=Table.csv,
    row=2,
    lower whisker=Min,
    lower quartile= Lower,
    median= Median,
    upper quartile= Upper,
    upper whisker= Max,
  }, boxplot prepared
  ]
  coordinates {};

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

I am getting the following error:

Package pgfkeys Error: I do not know the key '/tikz/boxplot prepared from tab
le', to which you passed 'table=Table.csv, row=1, lower whisker=Min, lower quar
tile= Lower, median= Median, upper quartile= Upper, upper whisker= Max,', and I
am going to ignore it. Perhaps you misspelled it.
2
  • What doesn‘t work well?
    – MS-SPO
    Feb 24 at 15:39
  • 1
    Sorry, I will put the error message I am getting. Nothing works. Feb 24 at 15:46

1 Answer 1

1

Well, if you look closely to the post you refer to, you will see that it provides a definition for the option boxplot prepared from table which is not defined by default. Hence, you need to copy this definition to your code as well, otherwise PGF will complain.

Also, you cannot directly read the contents from the CSV, but you need to parse it via \pgfplotstableread. I am unsure why that is, to be honest.

Apart from that, you could clean up your preamble a bit, since pgfplotstable already loads pgfplots. I think, you also need to adjust the ticks on the y axis, which I leave to you.

\documentclass{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}

% credit to https://tex.stackexchange.com/a/117439/47927
\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
% ===

\begin{filecontents}[noheader]{Table.csv}
Product,Min,Lower,Median,Upper,Max
Blue,12.5,16,25,32,35
Green,28,32.2,35.5,37,45
Yellow,6.6,15,25.5,30.2,70
Magenta,10,12.2,14.4,16.6,19.1
\end{filecontents}

\pgfplotstableread[col sep=comma]{Table.csv}\mydata

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    boxplot/draw direction=y,
    ytick={1,2,3,4},
    yticklabels={Blue,Green,Yellow,Magenta}
]
%Blue
\addplot+[
    boxplot prepared from table={
        table=\mydata,
        row=1,
        lower whisker=Min,
        lower quartile= Lower,
        median= Median,
        upper quartile= Upper,
        upper whisker= Max,
    }, boxplot prepared
]
coordinates {};
%Green
\addplot+[
    boxplot prepared from table={
        table=\mydata,
        row=2,
        lower whisker=Min,
        lower quartile= Lower,
        median= Median,
        upper quartile= Upper,
        upper whisker= Max,
    }, boxplot prepared
]
coordinates {};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

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