[Tex/LaTex] How to change the color of average in boxplot n pgfplots

boxplotcolorpgfplots

I have a simple graph using boxplot and I'm trying to customize the look. I'm part way there, but am having trouble with the average.
What I have so farwhere I have changed the color of the box and the median, but the average is kind of lost. I changed the box using:

\pgfplotsset{
   boxplot/draw/box/.code={%
   \draw[/pgfplots/boxplot/every box/.try,color=.!50!white]
     (boxplot box cs:\pgfplotsboxplotvalue{lower quartile},0)
     rectangle
     (boxplot box cs:\pgfplotsboxplotvalue{upper quartile},1)
     ;
   },%
 }

as per the manual (with the addition of the ",color=.!50!white") and the median similarly, but when I try the same thing with average,

\makeatletter 
\pgfplotsset{
  boxplot/draw/average/.code={%
    \draw[/pgfplots/boxplot/every average/.try,fill=red]
        \pgfextra
        \pgftransformshift{%
          \pgfplotsboxplotpointabbox
                {\pgfplotsboxplotvalue{average}}
                {0.5}%
        }%
        \pgfuseplotmark{\tikz@plot@mark}%
        \endpgfextra
        ;
     },
}
\makeatother

nothing changes. I tried many other things, including adding:

\addplot+[boxplot prepared={ % IE11 dotget
  draw position=1,
  every average/.style={fill=red,mark={diamond*}},
  average=100.2, lower whisker=98.1, lower quartile=98.4, median=100.5, upper quartile=101.2, upper whisker=101.5}]
 coordinates { };

but still no joy.

Thanks for any help! ../Dave

Best Answer

Simply add a \color command before the \draw command to achieve your desired result.

(Have a look at the code below which gives an example by using code from the PGFPlots manual and adding the color line.)

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{
        pgfplots.statistics,
    }
    \makeatletter
    \pgfplotsset{
        % example copied from the manual
        boxplot/draw/average/.code={%
                \color{.!50!green}          % <-- added
            \draw[/pgfplots/boxplot/every average/.try]
                \pgfextra
                % do NOT use \draw[mark=*] plot coordinates because
                % boxplots uses the same plot handler to draw its
                % outliers.
                \pgftransformshift{%
                    \pgfplotsboxplotpointabbox
                        {\pgfplotsboxplotvalue{average}}
                        {0.5}%
                }%
                \pgfuseplotmark{\tikz@plot@mark}%
                \endpgfextra
            ;
        },
    }
    \makeatother
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
    ]
        \addplot+ [
            boxplot prepared={
                average=100.2,
                lower whisker=98.1,
                lower quartile=98.4,
                median=100.5,
                upper quartile=101.2,
                upper whisker=101.5,
            },
        ] coordinates { };
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Related Question