[Tex/LaTex] pgfplots – draw boxplot without outliers

boxplotpgfplotsstatistics

I am creating a boxplot with pgfplots, using the builtin statistics library. I load the input data from a table file.

\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
...
\begin{tikzpicture}
\begin{axis}[boxplot/draw direction=y]
   \addplot+[boxplot] table[
     col sep=tab,
     x expr=\coordindex,
     y=aColName] {aFileName};
\end{axis}
\end{tikzpicture}

Is it possible to draw the plot without outliers? Or hide them? I don't want to pre-calculate the boxplot values by hand.

Best Answer

If you add the following code snippet to your preamble, you can use hide outliers to locally disable plotting the outliers:

\makeatletter
\pgfplotsset{
    boxplot/hide outliers/.code={
        \def\pgfplotsplothandlerboxplot@outlier{}%
    }
}
\makeatother

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\begin{document}

\makeatletter
\pgfplotsset{
    boxplot/hide outliers/.code={
        \def\pgfplotsplothandlerboxplot@outlier{}%
    }
}
\makeatother


\begin{tikzpicture}
\begin{axis}[y=1cm, try min ticks=2]
\addplot+[boxplot]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
21\\
};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[y=1cm, try min ticks=2]
% somewhen the simple statement of `hide outliers` broke.
% Now the full path to the style needs to be written.
\addplot+[boxplot, /pgfplots/boxplot/hide outliers]
table[row sep=\\,y index=0] {
data\\
1\\ 2\\ 1\\ 5\\ 4\\ 10\\
7\\ 10\\ 9\\ 8\\ 9\\ 9\\
21\\
};
\end{axis}
\end{tikzpicture}


\end{document}