1

Some of the box plots are shifted. Can you please help me to solve it?

(code is copied from user Korijn).

\documentclass[a4paper]{standalone}

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

\begin{document}

    \begin{tikzpicture}
    \begin{axis}[
    boxplot/draw direction=y,
    ylabel={AAAAA},
    height=6cm,
    ymin=0,ymax=7,
    cycle list={{red},{black}},
    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.2
    },
    % ... it also means that 1 unit in x controls the width:
    x=1cm,
    % ... and it means that we should describe intervals:
    xtick={0,1,2,...,20},
    x tick label as interval,
    xticklabels={%
        {0.15},%
        {0.2},%
        {0.25},%
        {0.3},%
        {0.4},%
        {0.5},%
        {0.6},%
        {0.8},%
        {1.0},
    },
    x tick label style={
        text width=2.5cm,
        align=center
    },
    ]
    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.764\\
        2.938\\
        2.075\\
        1.493\\
        1.285\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.175\\
        2.813\\
        2.006\\
        3.893\\
        2.012\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.621\\
        3.659\\
        6.357\\
        2.851\\
        1.416\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.280\\
        1.482\\
        1.787\\
        2.326\\
        1.795\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.778\\
        2.388\\
        1.016\\
        1.328\\
        1.151\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.028\\
        1.571\\
        4.090\\
        3.875\\
        1.890\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.405\\
        1.188\\
        4.330\\
        3.665\\
        1.439\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.937\\
        1.320\\
        1.357\\
        1.852\\
        1.215\\
    };
    %----------------------%
    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.778\\
        2.388\\
        1.016\\
        1.328\\
        1.151\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.028\\
        1.571\\
        4.090\\
        3.875\\
        1.890\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.405\\
        1.188\\
        4.330\\
        3.665\\
        1.439\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.937\\
        1.320\\
        1.357\\
        1.852\\
        1.215\\
    };

%------------------------------%

        \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.405\\
        1.188\\
        4.330\\
        3.665\\
        1.439\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.937\\
        1.320\\
        1.357\\
        1.852\\
        1.215\\
    };
    %----------------------%
    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.778\\
        2.388\\
        1.016\\
        1.328\\
        1.151\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.028\\
        1.571\\
        4.090\\
        3.875\\
        1.890\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.405\\
        1.188\\
        4.330\\
        3.665\\
        1.439\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.937\\
        1.320\\
        1.357\\
        1.852\\
        1.215\\
    };


    \end{axis}
    \end{tikzpicture}


\end{document}

enter image description here

Edit by ManuelKuehner

Code based on here ans menioned in the comments.

user3104352
  • 323
  • 2
  • 11
  • I can't compile your code please edit your question. Do you need alway groups with 2 boxes, pairs? – Bobyandbob Aug 20 '17 at 05:23
  • yes I always need 2 boxes. – user3104352 Aug 20 '17 at 05:40
  • Can you check it please? I edited it. – user3104352 Aug 20 '17 at 05:42
  • 2
    I tried, but at the moment i can't solve it. I thing the problem is draw position={1/3 + floor(\plotnumofactualtype/2) + 1/3*mod(\plotnumofactualtype,2)},. I don't know \plotnumofactualtype. Your addplot data do not tangent the problem. Your code is from Christian Feuersänger (please ref.) – Bobyandbob Aug 20 '17 at 06:19
  • Code from other should be attributed as mentioned above. Also, do you no longer need your earlier question? These seem to be grouped. If so, please delete that one so people don't spend time on it. Or can it be closed as a duplicate of another question you found later? – cfr Aug 20 '17 at 13:46
  • @Bobyandbob Looks helpful, but what do you mean by 'do not tangent the problem'? – cfr Aug 20 '17 at 13:47
  • @cfr ups...nothing. Wrong place for this, if i could i would edit the comment. It refers to an other question. Copy and paste failed here. Sorry. Do you know something about \plotnumofactualtype? – Bobyandbob Aug 20 '17 at 14:01
  • @Bobyandbob Sorry, no - no clue at all. – cfr Aug 20 '17 at 20:28

1 Answers1

1

I have found a solution from this post. I adapted it to my code. It seems the problem is because of a bug in mod function. The problem is solved in

\documentclass[a4paper]{standalone}

\usepackage{pgfplots}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.8}
\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{3}}{}%
}
\begin{document}

    \begin{tikzpicture}
    \begin{axis}[
    boxplot/draw direction=y,
    ylabel={AAAAA},
    height=6cm,
    ymin=0,ymax=7,
    cycle list={{red},{black}},
    boxplot={
        draw position={1/3 + floor(\plotnumofactualtype/2) + 1/3*fpumod(\plotnumofactualtype,2)},
        box extend=0.15
    },
    % ... it also means that 1 unit in x controls the width:
    x=1cm,
    % ... and it means that we should describe intervals:
    xtick={0,1,2,...,20},
    x tick label as interval,
    xticklabels={%
        {0.15},%
        {0.2},%
        {0.25},%
        {0.3},%
        {0.4},%
        {0.5},%
        {0.6},%
        {0.8},%
        {1.0},
    },
    x tick label style={
        text width=2.5cm,
        align=center
    },
    ]
    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.764\\
        2.938\\
        2.075\\
        1.493\\
        1.285\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.175\\
        2.813\\
        2.006\\
        3.893\\
        2.012\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.621\\
        3.659\\
        4.357\\
        2.851\\
        1.416\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.280\\
        1.482\\
        1.787\\
        2.326\\
        1.795\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.778\\
        2.388\\
        1.016\\
        1.328\\
        1.151\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.028\\
        1.571\\
        4.090\\
        3.875\\
        1.890\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.405\\
        1.188\\
        4.330\\
        3.665\\
        1.439\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.937\\
        1.320\\
        1.357\\
        1.852\\
        1.215\\
    };
    %----------------------%
    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.778\\
        2.388\\
        1.016\\
        1.328\\
        1.151\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.028\\
        1.571\\
        4.090\\
        3.875\\
        1.890\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.405\\
        1.188\\
        4.330\\
        3.665\\
        1.439\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.937\\
        1.320\\
        1.357\\
        1.852\\
        1.215\\
    };

%------------------------------%

        \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.405\\
        1.188\\
        4.330\\
        3.665\\
        1.439\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.937\\
        1.320\\
        1.357\\
        1.852\\
        1.215\\
    };
    %----------------------%
    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.778\\
        2.388\\
        1.016\\
        1.328\\
        1.151\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.028\\
        1.571\\
        4.090\\
        3.875\\
        1.890\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        1.405\\
        1.188\\
        4.330\\
        3.665\\
        1.439\\
    };

    \addplot
    table[row sep=\\,y index=0] {
        data\\
        2.937\\
        1.320\\
        1.357\\
        1.852\\
        1.215\\
    };
    \end{axis}
    \end{tikzpicture}
\end{document}
user3104352
  • 323
  • 2
  • 11