[Tex/LaTex] PGF boxplot with annotations

annotationsboxplotpgfplots

I read the section about boxplots in the pgfplots manual but I cant get it to work.
I need to display the median inside the boxplot above or below the line of the median.
How can I add the values to the Plot?
Here is my code so far:

\documentclass[12pt,tikz,border=10pt]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.statistics}


\begin{document}


\begin{tikzpicture}
\begin{axis}[
width  = .5\textwidth,
height = 12cm,
axis x line*=bottom,
axis y line=left,
enlarge y limits,
ymajorgrids,
major x tick style = transparent,
ylabel = {Time},
ymin=8.15,
ymax=40,
xmin=0,
xmax=3,
xticklabels={,,},
xtick={1,...,3},
boxplot/draw direction=y,
]

\addplot+[boxplot prepared={
    lower whisker=9.06, lower quartile=9.29,
    median=12.10,
    upper quartile=13.125, upper whisker=14.05,
    box extend=0.4}, color=blue
]
coordinates {};


\addplot+[boxplot prepared={
    lower whisker=19.22, lower quartile=20.255,
    median=21.58,
    upper quartile=30.15, upper whisker=30.15,
    box extend=0.4}, color=red]
coordinates {(0, 37.05)};

\end{axis}

\end{tikzpicture}
\end{document}

Best Answer

Right after the coordinates for the boxplot, add the code for one of the nodes found in the example in the manual. I just changed left to above/below, and reduced the inner sep to get the number closer to the line.

\documentclass[12pt,tikz,border=10pt]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.statistics}

\begin{document}   
\begin{tikzpicture}
\begin{axis}[
width  = .5\textwidth,
height = 12cm,
axis x line*=bottom,
axis y line=left,
enlarge y limits,
ymajorgrids,
major x tick style = transparent,
ylabel = {Time},
ymin=8.15,
ymax=40,
xmin=0,
xmax=3,
xticklabels={,,},
xtick={1,...,3},
boxplot/draw direction=y,
]

\addplot+[boxplot prepared={
    lower whisker=9.06, lower quartile=9.29,
    median=12.10,
    upper quartile=13.125, upper whisker=14.05,
    box extend=0.4}, color=blue
]
coordinates {}
node[above,inner sep=1pt,font=\tiny] at
(boxplot box cs: \boxplotvalue{median},0.5)
{\pgfmathprintnumber{\boxplotvalue{median}}};;


\addplot+[boxplot prepared={
    lower whisker=19.22, lower quartile=20.255,
    median=21.58,
    upper quartile=30.15, upper whisker=30.15,
    box extend=0.4}, color=red]
coordinates {(0, 37.05)}
node[above,inner sep=1pt,font=\tiny] at
(boxplot box cs: \boxplotvalue{median},0.5)
{\pgfmathprintnumber{\boxplotvalue{median}}};

\end{axis}

\end{tikzpicture}
\end{document}

enter image description here

Related Question