1

I am not sure why after the 6th group of data, the boxes start to be placed to the right (wrong position).

I would like to remove this white space at the start and end (lines in red).

enter image description here

% 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}}{}%
    }

The chart

\begin{figure*}
    \centering
        \begin{tikzpicture}
            \begin{axis}[
                boxplot/draw direction=y,
                ylabel={CPU Time (ms)},
                height=4cm,
                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 4 at 3,2 + 3,4 + 3,6 + 3,8
                    %
                    % in a formular:
                    draw position={
                        1/3 + floor(\plotnumofactualtype/2) + 1/3*fpumod(\plotnumofactualtype,2)
                    },
                    % that means the box extend must be at most 0.2 :
                    box extend=0.3,
                },
                % ... it also means that 1 unit in x controls the width:
                x={1.4cm},
                % ... and it means that we should describe intervals:
                xtick={0,1,2,...,7},
                x tick label as interval,
                xticklabels={%
                  {YFJS01},%{YFJS01\\{\tiny off/on}},%
                  {YFJS02},%
                  {YFJS03},%
                  {YFJS04},%
                  {YFJS05},%
                  {YFJS06},%
                  {YFJS07},%
                  {YFJS08},%
                  {YFJS09},%
                  {YFJS10},%
                },
                x tick label style={
                  text width=1.5cm,
                  align=center
                },
                cycle list={{red},{blue}},%,{colorw_3},{colorw_4}},
            ]
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                }; 

            % block 2
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                }; 

            % block 3
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                }; 

            % block 4
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                }; 
            % block 5
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                }; 
            % block 6
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                }; 
            % block 6
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };  
            \end{axis}
    \end{tikzpicture}
    \caption{Caption}
    \label{fig:my_label}
\end{figure*}
  • 1
    Can you make your example compilable? It includes undefined colours (in the cycle list) and an undefined function (fpumod). – Torbjørn T. May 28 '18 at 9:48
  • I changed the custom colors to red and blue. – WillEnsaba May 28 '18 at 9:56
  • 1
    And fpumod? Even better if you add a minimal preamble with documentclass, the necessary packages and libraries, and a document environment, so the code can be compiled directly without modification. – Torbjørn T. May 28 '18 at 9:57
  • I added the fpumod. – WillEnsaba May 28 '18 at 9:59
  • I found the problem for the misplacing. The problem was at the fpumod (pgfmathfloatifapproxequalrel, replaced 5 by 3). The only problem now is to reduce the white space at the start and end. – WillEnsaba May 28 '18 at 10:09
2

I see you found the answer to the main problem yourself, so that's implemented here (change 5 to 3 in the definition of fpumod).

For the spacing, you can set the x-limits explicitly with xmin=0,xmax=7, or you can adjust the spacing with enlarge x limits={abs=0.3}. Change 0.3 to what you deem suitable.

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
% 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 `3' to make it work for this problem
        \pgfmathfloatifapproxequalrel{\pgfmathresult}{#2}{\def\pgfmathresult{3}}{}%
    }
\begin{document}
\begin{figure*}
    \centering
        \begin{tikzpicture}
            \begin{axis}[
%                xmin=0,xmax=7,
                enlarge x limits={abs=0.3},
                boxplot/draw direction=y,
                ylabel={CPU Time (ms)},
                height=4cm,
                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 4 at 3,2 + 3,4 + 3,6 + 3,8
                    %
                    % in a formular:
                    draw position={
                        1/3 + floor(\plotnumofactualtype/2) + 1/3*fpumod(\plotnumofactualtype,2)
                    },
                    % that means the box extend must be at most 0.2 :
                    box extend=0.3,
                },
                % ... it also means that 1 unit in x controls the width:
                x={1.4cm},
                % ... and it means that we should describe intervals:
                xtick={0,1,2,...,7},
                x tick label as interval,
                xticklabels={%
                  {YFJS01},%{YFJS01\\{\tiny off/on}},%
                  {YFJS02},%
                  {YFJS03},%
                  {YFJS04},%
                  {YFJS05},%
                  {YFJS06},%
                  {YFJS07},%
                  {YFJS08},%
                  {YFJS09},%
                  {YFJS10},%
                },
                x tick label style={
                  text width=1.5cm,
                  align=center
                },
                cycle list={{red},{blue}},%,{colorw_3},{colorw_4}},
            ]
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                }; 

            % block 2
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                }; 

            % block 3
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                }; 

            % block 4
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                }; 
            % block 5
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                }; 
            % block 6
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                }; 
            % block 6
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };
                \addplot table [row sep=\\,y index=0] {
                    data\\
                    200\\
                    205\\
                    209\\
                    205\\
                    210\\
                };  

            \end{axis}
    \end{tikzpicture}
    \caption{Caption}
    \label{fig:my_label}
\end{figure*}
\end{document}
  • Maybe you can help with one last minor question. How can I align the x tick with the labels? I mean, for each label I would like one tick aligned to it. – WillEnsaba May 30 '18 at 9:01
  • 1
    @WillEnsaba You have explicitly told pgfplots to put ticklabels between ticks with x tick label as interval. So remove that. Then, depending on exactly what output you're after, you might want to use xtick={0,...,6} (so you only have seven ticks, not eight), and change the first 1/3 in the draw position to -1/6 (so the groups are centered on the ticks). – Torbjørn T. May 30 '18 at 9:25

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.