8

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.

3 Answers 3

7

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}
3
  • 1
    I tried your sample both on my local Latex as well as on Overleaf and I always get Package pgfkeys Error: I do not know the key '/tikz/hide outliers' and I amd going to ignore it.
    – schneida
    Mar 31, 2019 at 13:01
  • You can also do boxplot={hide outliers} or stick boxplot/hide outliers into the optional arguments of axes, instead of using boxplot, /pgfplots/boxplot/hide outliers Jul 29, 2022 at 20:16
  • This does not seem to work with \pgfplotsset{compat=1.16}... Jul 29, 2022 at 20:34
1

Just for completeness here is a solution using the combination of 'R-knitr-LaTeX'.

\documentclass[border=5mm]{standalone}
\begin{document}
<<echo=FALSE>>=
#par(pin=c(5,.1))
#par(mar=c(.2,.2,.2,.2))
par(bty="n")
#par(mai=c(.35,.35,.35,.35),omi=c(0,0,0,0))
par(mfrow=c(2,1))
x=c(1,2,1,5,4,10,7,10,9,8,9,9,21)
boxplot(x,horizontal=TRUE,outline=TRUE,plot=TRUE,axes=TRUE)
axis(side=1, at=c(0,5,10,15,20))
boxplot(x,horizontal=TRUE,outline=FALSE,plot=TRUE, col='lightblue',axes=TRUE)
axis(side=1, at=c(0,2,4,6,8,10))
@
\end{document}

enter image description here

1

I have simply done that by just adding mark=none to \addplot+[boxplot]

\begin{tikzpicture}
\begin{axis}[boxplot/draw direction=y,]

\addplot+[boxplot]
table[row sep=\\,y index=0] {
    data\\
    1\\ 2\\ 1\\ 5\\ 4\\ 10\\
    7\\ 10\\ 9\\ 8\\ 9\\ 9\\
    21\\
};

% box plot without outliers
\addplot+[boxplot, mark=none]
table[row sep=\\,y index=0] {
    data\\
    1\\ 2\\ 1\\ 5\\ 4\\ 10\\
    7\\ 10\\ 9\\ 8\\ 9\\ 9\\
    21\\
};
\end{axis}
\end{tikzpicture}

enter image description here

1
  • This is not a valid solution. The marks are not drawn, but still considered for the y-axis. So in the example, even if only the red boxplot is drawn the y-axis would still reach past 20, leaving half the plot empty
    – rzippo
    Nov 4, 2021 at 9:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .