-3

My CSV looks like:

year,min,0.1,0.5,0.75,max,mean,totals
1992,4,200,400,780,1500,600,2345
1993,8,400,800,1560,3000,1200,4680

I'm after a box plot of this with pgfplots, however, the documentation is so large and I don't have the time to spend a few days working to even get a MWE. Could somebody provide an MWE I can work from?

If possible I'd also like to throw a trend line between the mean values of each plot too, and add a single integer value which I manually plot (i.e. not from CSV) to show an outlier.

Timmy
  • 95

1 Answers1

2

I don't see how the total column is needed for a boxplot but anyway, here's an adaptation of what you're asking based on this answer, this answer and this this answer.

Code

\documentclass[crop=false]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=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[col sep = comma]{data.csv}\datatable

\begin{document} \begin{tikzpicture} \pgfplotstablegetrowsof{\datatable} \pgfmathtruncatemacro\TotalRows{\pgfplotsretval-1}

\begin{axis}[boxplot/draw direction=y, xticklabels from table = {\datatable}{year}, xtick = {1,...,\TotalRows+1} ]

\pgfplotsinvokeforeach{0,...,\TotalRows} { \addplot+[ boxplot prepared from table={ table=\datatable, row=#1, lower whisker=min, upper whisker=max, lower quartile=0.1, upper quartile=0.75, median=0.5 }, boxplot prepared] coordinates {}; };

\addplot table [x expr =\coordindex+1,y index=6] {\datatable}; \node[label={225:{Outlier}},circle,fill,inner sep=1.5pt] at (axis cs:2,3000) {}; \legend{};

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

Output

LaTeX output

Enevevet
  • 772