2

In the following, you find a modified version of the boxplot from here (4 boxplots per data set instead of 2):

\documentclass[a4paper]{standalone}

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

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  boxplot/draw direction=y,
  ylabel={time (s)},
  height=8cm,
  boxplot={
      %
      % Idea: 
      %  place the 
      %  group 1 at 0,2 + 0,4 + 0,6 + 0,8
      %  group 2 at 1,2 + 1,4 + 1,6 + 1,8
      %  group 3 at 2,2 + 2,4 + 2,6 + 2,8
      %  group 3 at 3,2 + 3,4 + 3,6 + 3,8
      %  
      % in a formular:
      draw position={1/5 + floor(\plotnumofactualtype/4) + 1/5*mod(\plotnumofactualtype,4)},
      %
      % that means the box extend must be at most 0.2 :
      box extend=0.15,
  },
  % ... it also means that 1 unit in x controls the width:
  x=3cm,
  % ... and it means that we should describe intervals:
  xtick={0,1,2,...,50},
  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
  },
  cycle list={{red},{blue},{green},{purple}},
]

\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\\
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\\
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\\
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\\
};


\end{axis}
\end{tikzpicture}

\end{document}

Unfortunately, one boxplot is at a wrong position:

enter image description here

I expect this boxplot to be at position 3.2. Why is it on 3.0?

[EDIT] My workaround is to set that position manually:

\addplot+[boxplot={draw position=3.2}]

1 Answer 1

3

Addendum/New answer

With the release of TikZ/PGF v3.1 the below mentioned bug is fixed and thus OPs example now works as expected.


Old answer

This is because of an already known bug in the mod function. To circumvent the problem I adjusted a solution that was originally given by Jake.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{statistics}
    \pgfplotsset{compat=1.8}
    % borrowed from <https://tex.stackexchange.com/a/145967/95441>
    \pgfmathdeclarefunction{fpumod}{2}{%
        \pgfmathfloatdivide{#1}{#2}%
        \pgfmathfloatint{\pgfmathresult}%
        \pgfmathfloatmultiply{\pgfmathresult}{#2}%
        \pgfmathfloatsubtract{#1}{\pgfmathresult}%
        % replaced `0' by `5' to make it work for this problem
        \pgfmathfloatifapproxequalrel{\pgfmathresult}{#2}{\def\pgfmathresult{5}}{}%
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        boxplot/draw direction=y,
        ylabel={time (s)},
        height=8cm,
        boxplot={
            %
            % Idea:
            %  place the
            %  group 1 at 0,2 + 0,4 + 0,6 + 0,8
            %  group 2 at 1,2 + 1,4 + 1,6 + 1,8
            %  group 3 at 2,2 + 2,4 + 2,6 + 2,8
            %  group 3 at 3,2 + 3,4 + 3,6 + 3,8
            %
            % in a formular:
            draw position={
                1/5 + floor(\plotnumofactualtype/4)
                  + 1/5*fpumod(\plotnumofactualtype,4)
            },
            % that means the box extend must be at most 0.2 :
            box extend=0.15,
        },
        % ... it also means that 1 unit in x controls the width:
        x=3cm,
        % ... and it means that we should describe intervals:
        xtick={0,1,2,...,50},
%        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
%        },
        cycle list={{red},{blue},{green},{purple}},
    ]
        \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\\
        };

    % block 2
        \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\\
        };

    % block 3
        \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\\
        };

    % block 4
        \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\\
        };
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

2
  • For some reason, in the environment on overleaf.com, \pgfmathparse{mod(15,5)}\pgfmathresult gives 0.0 as expected, but OP's example is still bugged. In fact, when I use \pgfmathparse{...}\pgfmathresult to evaluate OP's expression for draw position, it gives 3.2, even though the box is evidently drawn at x=3.0... Jun 18, 2021 at 1:50
  • Sorry, I don't know anything about overleaf. I suggest you ask on a suitable overleaf channel for help. Good luck! Jun 19, 2021 at 20:11

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.