7

Currently I have the following MWE:

\documentclass[a4paper]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{external,colormaps,groupplots,statistics}
\pgfplotsset{compat=1.8}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}



\begin{document}

\begin{tikzpicture}
\begin{groupplot}[
boxplot/draw direction=y,
ylabel={segmentation time (secs)},
xtick={1,2},
width=6cm,
height=8cm,
boxplot/box extend=0.5,
xticklabels={visualization disabled,visualization enabled},
    x tick label style={
        text width=2.5cm,
        align=center
    },
      group style={
        group size=2 by 2,
          horizontal sep=2cm,vertical sep=2cm
      },
]

\nextgroupplot[title=dataset 1]
\addplot+[boxplot]
table[row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot+[boxplot]
table[row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};

\nextgroupplot[title=dataset 2]
\addplot+[boxplot]
table[row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot+[boxplot]
table[row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};

\nextgroupplot[title=dataset 3]
\addplot+[boxplot]
table[row sep=\\,y index=0] {
data\\
956\\
320\\
811\\
330\\
381\\
};
\addplot+[boxplot]
table[row sep=\\,y index=0] {
data\\
280\\
749\\
392\\
870\\
488\\
};

\nextgroupplot[title=dataset 4]
\addplot+[boxplot]
table[row sep=\\,y index=0] {
data\\
658\\
579\\
891\\
545\\
558\\
};
\addplot+[boxplot]
table[row sep=\\,y index=0] {
data\\
514\\
630\\
416\\
559\\
462\\
};

\end{groupplot}
\end{tikzpicture}


\end{document}

Which nets me the following output (which is usable I guess):

Example

I would like to have them all in a single picture though. Kind of like the way you can group the boxes in box charts. So, to describe it more precisely:

  • X ticks are dataset 1 through 4
  • On every x tick are two box plots, one for disabled and one for enabled
  • For clarity each pair of boxplots should be visually grouped together (maybe shift them towards eachother?)

I have been searching through the PGFPlots manuals and web but haven't found anything that works so far. Can anyone help me out?

1
  • This question need new answers to achieve the effect.
    – Y. zeng
    Commented Sep 22, 2023 at 1:52

1 Answer 1

12

Box plots are always placed at the coordinate specified with boxplot/draw position. This, in turn, defaults to 1+\plotnumofactualtype, but it can be customized to other values.

In your case, you can simply place all your plots into one axis and assign an individual boxplot/draw position to each.

Here is an approach which defines a math expression which appears to be close to what you need (comments are in the code):

enter image description here

\documentclass[a4paper]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
boxplot/draw direction=y,
ylabel={segmentation time (secs)},
height=8cm,
boxplot={
    %
    % Idea: 
    %  place the 
    %  group 1 at 0.3333 and 0.6666
    %  group 2 at 1.3333 and 1.6666
    %  group 3 at 2.3333 and 2.6666
    %  ...
    % in a formular:
    draw position={1/3 + floor(\plotnumofactualtype/2) + 1/3*mod(\plotnumofactualtype,2)},
    %
    % that means the box extend must be at most 0.33333 :
    box extend=0.3,
},
% ... it also means that 1 unit in x controls the width:
x=2cm,
% ... and it means that we should describe intervals:
xtick={0,1,2,...,10},
x tick label as interval,
xticklabels={%
    {Data set 1\\{\tiny off/on}},%
    {Data set 2\\{\tiny off/on}},%
    {Data set 3\\{\tiny off/on}},%
    {Data set 4\\{\tiny off/on}},%
},
    x tick label style={
        text width=2.5cm,
        align=center
    },
]

\addplot
table[row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
956\\
320\\
811\\
330\\
381\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
280\\
749\\
392\\
870\\
488\\
};

\addplot
table[row sep=\\,y index=0] {
data\\
658\\
579\\
891\\
545\\
558\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
514\\
630\\
416\\
559\\
462\\
};

\end{axis}
\end{tikzpicture}


\end{document}

You may need to adopt the cycle list for suitable line styles.

1

You must log in to answer this question.

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