4

I would like to use pgfplots to draw boxplots. This minimal example works fine for me.

But I would like to display more data in this form

\begin{filecontents}{testdata.dat}
 0 1 1.2 0.4 1.5 0.2
 1 2 2.3 1.5 2.7 1
 2 0.7 1.4 0.5 1.9 0.1
 3 1 1.2 0.4 1.5 0.2
 4 2 2.3 1.5 2.7 1
 5 0.7 1.4 0.5 1.9 0.1
\end{filecontents}

When I run this minimal example mentioned above I get touching or overlapping boxes. Unfortunately I am not allowed to attach images. Just run the example below by yourself.

What do I need to adjust in the code provided, in order account for more data and remove overlapping graphs? Could this width be adjusted manually or is there even an option to do this automatically?

\documentclass{minimal}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{testdata.dat}
 0 1 1.2 0.4 1.5 0.2
 1 2 2.3 1.5 2.7 1
 2 0.7 1.4 0.5 1.9 0.1
 3 1 1.2 0.4 1.5 0.2
 4 2 2.3 1.5 2.7 1
 5 0.7 1.4 0.5 1.9 0.1
\end{filecontents}

\pgfplotsset{
 box plot/.style={
    /pgfplots/.cd,
    black,
    only marks,
    mark=-,
    mark size=1em,
    /pgfplots/error bars/.cd,
    y dir=plus,
    y explicit,
 },
 box plot box/.style={
    /pgfplots/error bars/draw error bar/.code 2 args={%
        \draw  ##1 -- ++(1em,0pt) |- ##2 -- ++(-1em,0pt) |- ##1 -- cycle;
    },
    /pgfplots/table/.cd,
    y index=2,
    y error expr={\thisrowno{3}-\thisrowno{2}},
    /pgfplots/box plot
 },
 box plot top whisker/.style={
    /pgfplots/error bars/draw error bar/.code 2 args={%
        \pgfkeysgetvalue{/pgfplots/error bars/error mark}%
        {\pgfplotserrorbarsmark}%
        \pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
        {\pgfplotserrorbarsmarkopts}%
        \path ##1 -- ##2;
    },
    /pgfplots/table/.cd,
    y index=4,
    y error expr={\thisrowno{2}-\thisrowno{4}},
    /pgfplots/box plot
 },
 box plot bottom whisker/.style={
    /pgfplots/error bars/draw error bar/.code 2 args={%
        \pgfkeysgetvalue{/pgfplots/error bars/error mark}%
        {\pgfplotserrorbarsmark}%
        \pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
        {\pgfplotserrorbarsmarkopts}%
        \path ##1 -- ##2;
    },
    /pgfplots/table/.cd,
    y index=5,
    y error expr={\thisrowno{3}-\thisrowno{5}},
    /pgfplots/box plot
 },
 box plot median/.style={
    /pgfplots/box plot
 }
}

\begin{document}
\begin{tikzpicture}
\begin{axis} [enlarge x limits=0.5,xtick=data]
 \addplot [box plot median] table {testdata.dat};
 \addplot [box plot box] table {testdata.dat};
 \addplot [box plot top whisker] table {testdata.dat};
 \addplot [box plot bottom whisker] table {testdata.dat};
\end{axis}
\end{tikzpicture}
\end{document}

Thank you very much for your time.

1
  • My search for a way to resize the width of my boxplots let me to this questions. It might be interesint for people who use boxplots differently (e.g. simple addplot+ in an axis, as recommended in the docs) that you can decrease the with by setting the boxplot/box extend key to 0.x.
    – thorsten
    Commented Jun 28, 2016 at 14:25

1 Answer 1

4

Here's your example with a new optional key box plot width, which can be used to set the widths of the boxes:

\documentclass{minimal}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{testdata.dat}
 0 1 1.2 0.4 1.5 0.2
 1 2 2.3 1.5 2.7 1
 2 0.7 1.4 0.5 1.9 0.1
 3 1 1.2 0.4 1.5 0.2
 4 2 2.3 1.5 2.7 1
 5 0.7 1.4 0.5 1.9 0.1
\end{filecontents}

\pgfplotsset{
 box plot width/.initial=1em,
 box plot/.style={
    /pgfplots/.cd,
    black,
    only marks,
    mark=-,
    mark size=\pgfkeysvalueof{/pgfplots/box plot width},
    /pgfplots/error bars/.cd,
    y dir=plus,
    y explicit,
 },
 box plot box/.style={
    /pgfplots/error bars/draw error bar/.code 2 args={%
        \draw  ##1 -- ++(\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##2 -- ++(-\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##1 -- cycle;
    },
    /pgfplots/table/.cd,
    y index=2,
    y error expr={\thisrowno{3}-\thisrowno{2}},
    /pgfplots/box plot
 },
 box plot top whisker/.style={
    /pgfplots/error bars/draw error bar/.code 2 args={%
        \pgfkeysgetvalue{/pgfplots/error bars/error mark}%
        {\pgfplotserrorbarsmark}%
        \pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
        {\pgfplotserrorbarsmarkopts}%
        \path ##1 -- ##2;
    },
    /pgfplots/table/.cd,
    y index=4,
    y error expr={\thisrowno{2}-\thisrowno{4}},
    /pgfplots/box plot
 },
 box plot bottom whisker/.style={
    /pgfplots/error bars/draw error bar/.code 2 args={%
        \pgfkeysgetvalue{/pgfplots/error bars/error mark}%
        {\pgfplotserrorbarsmark}%
        \pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
        {\pgfplotserrorbarsmarkopts}%
        \path ##1 -- ##2;
    },
    /pgfplots/table/.cd,
    y index=5,
    y error expr={\thisrowno{3}-\thisrowno{5}},
    /pgfplots/box plot
 },
 box plot median/.style={
    /pgfplots/box plot
 }
}

\begin{document}
\begin{tikzpicture}
\begin{axis} [enlarge x limits=0.5,xtick=data, box plot width=0.5em]
 \addplot [box plot median] table {testdata.dat};
 \addplot [box plot box] table {testdata.dat};
 \addplot [box plot top whisker] table {testdata.dat};
 \addplot [box plot bottom whisker] table {testdata.dat};
\end{axis}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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