4

I need a Box plot with two boxes on each ytick. Any ideas?

\begin{tikzpicture}
  \begin{axis} [
        title=Scenario 1,
        height=6.0cm, width=8.0cm,
        xmin=6, xmax=20, xtick={6,8,...,20},
    ytick={1,2,3},
    yticklabels={Mon, Tue, Wed},
        legend style={at={(0.5,-0.2)}, anchor=north, legend columns=-1},
    ]
    \addplot [
    boxplot prepared={
      upper quartile=15,
      lower quartile=8,
      upper whisker=19,
      lower whisker=7
    },
    ] coordinates {};
    \addplot [
    boxplot prepared={
      upper quartile=17,
      lower quartile=10,
      upper whisker=19,
      lower whisker=7
    },
    ] coordinates {};
  \end{axis}
\end{tikzpicture}
3

Perhaps something like this, though some manual fiddling is required. Start by reducing the height of the boxes, by adding /pgfplots/boxplot/box extend=0.3 to the axis options. The default value is 0.8, adjust this to your liking.

Then for each plot, set draw position to some value slightly above or below the tick value. E.g. draw position=1.2 and 0.8 as in this example.

enter image description here

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

\begin{tikzpicture}
  \begin{axis} [
        title=Scenario 1,
        height=6.0cm, width=8.0cm,
        xmin=6, xmax=20, xtick={6,8,...,20},
    ytick={1,2,3},
    yticklabels={Mon, Tue, Wed},
        legend style={at={(0.5,-0.2)}, anchor=north, legend columns=-1},
    ymin=0,ymax=4,
    /pgfplots/boxplot/box extend=0.3
    ]
    \addplot+ [
    boxplot prepared={
      upper quartile=15,
      lower quartile=8,
      upper whisker=19,
      lower whisker=7,
      draw position=1.2
    },
    ] coordinates {};
    \addplot+ [
    boxplot prepared={
      upper quartile=17,
      lower quartile=10,
      upper whisker=19,
      lower whisker=7,
      draw position=0.8
    },
    ] coordinates {};
  \end{axis}
\end{tikzpicture}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.