My question is somewhat related to this thread. I would like
to create box plots from a file. The file (in my case a CSV) looks like this:
Upper,Lower,Min,Max,Median,Name
3,1,0,4,2,First
4,2,1,5,3,Second
...
There are multiple rows, each of which contains fields describing the data points for the box plot (median, position of the whiskers and the box boundaries) and the name of the data set. I would like to create a number of box plots from this data. The name should be added as a legend entry.
I have tried using a CSV reader:
\begin{tikzpicture}
\begin{axis}[
\csvreader[
head to column names,
separator=comma]
{data.csv}{}% use head of csv as column names
{
\addplot+ [
boxplot prepared={
lower whisker=\Min,
lower quartile=\Lower,
median=\Median,
upper quartile=\Upper,
upper whisker=\Max,
},
] coordinates {};
}
\end{axis}
\end{tikzpicture}
This produces an empty plot though. My guess is that the macros (\Min
and so on) do not work properly in the tikzpicture
/ axis
environment?! (It works when I just dump out the values outside of the picture).
I also took a look at the thread mentioned above. The problem in this case is that only the first row of the table is plotted whereas I would like to plot all of them. This shortcoming is mentioned in the comments, but no extension of the approach is mentioned.
What is the best way to add the plots?
Best Answer
You can extend Jake's answer to Read boxplot prepared values from a table fairly easily, using
\pgfplotsinvokeforeach
.The code below is based on Jake's answer, with just a few modifications:
a way of getting the number of rows from a table:
Subtract one, because row/column numbers start counting from 0 in indexing.
a loop instead of two different
\addplot
s:Note the addition of
row=#1
in the boxplot options, in\pgfplotsinvokeforeach
the loop variable is represented by#1
.added
area legend
to the\addplot
options, otherwise the legend image is a complete (large) box plot.added legend entry from table: