1

I am not very familiar with box plots in pgfplots. But now I have to do some. The first turned out pretty well with 3 boxes and their corresponding data. I wanted to reuse this code for another box plot but with 2 boxes. Now 2 problems appeared.

  1. The data and the boxes are no longer overlapping, but shifted aside.

enter image description here

  1. After several attempts the only solution I could come up with is to add a dummy box and hide it with draw=none. But then I have this extra x axis tick which I was not able to get rid of it.

enter image description here

I basically do not quite understand how the position of the box is decided. I thought the 1 in box plot coordinates (1, 3.9) is the same coordinate (e.g. (1,4.1)) as in the mark plot. But it seems this is not the case.

Is there a better solution than adding a dummy box plot and how can I then get rid of the additional x tick?

Thanks.

MWE:

\documentclass[border=0.5cm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            title=\textbf{Title},
            legend style={at={(0.12,0.98)},anchor=north,font=\tiny},
            legend cell align={left},
            ybar=0.7cm,
            xticklabels={\textbf{data 1 [n=7]}, \textbf{data 2 [n=7]}},
            x tick label style={rotate=45, anchor=north east, inner sep=1mm,font=\small},
            ylabel={\textbf{\small concentration}},
            bar width=1cm,
            grid=none,
            ymin=0,
            ymax=15,
            ]
            
            \addplot[black,thick,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt},
            error mark options={
                rotate=90,
                mark size=5pt,
                line width=1pt
            }] 
                plot coordinates {(1, 3.9)+- (1.2,1.2)};
            \addplot[red,thick,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt},
            error mark options={
                rotate=90,
                mark size=5pt,
                line width=1pt
            }]  
            plot coordinates {(1, 1.6)+- (0.4,0.4)};
            
        %   \addplot[draw=none]
        %   plot coordinates {(1, 1)};
                    
            \legend{data 1, data 2}
            
            \addplot+[mark=o,only marks,xshift=-1.7cm,black] coordinates
            {(1,4.1) (1,1.2) (1,2.4) (1,13.1) (1,2.5) (1,2.0) (1,1.7)};
            \addplot+[mark=o,only marks,red] coordinates
            {(1,1.1) (1,2.0) (1,3.3) (1,2.5) (1,1.4) (1,2.2) (1,1.2)};          
        \end{axis}
    
    \end{tikzpicture}
\end{document}

2 Answers 2

2

Bar plots are automatically shifted, not to overlap. You can adjust their position with bar shift=....

I hope you have some very good reason to shift your plot by 1.7 cm. It is a very bad way to move data, that can only lead to problems.

\documentclass[border=0.5cm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            title=\textbf{Title},
            legend style={at={(0.12,0.98)},anchor=north,font=\tiny},
            legend cell align={left},
            ybar=0.7cm,
            x tick label style={rotate=45, anchor=north east, inner sep=1mm,font=\small},
            ylabel={\textbf{\small concentration}},
            bar width=1cm,
            grid=none,
            ymin=0,
            ymax=15,
            xtick= {-10,1},
            xticklabels={\textbf{data 1 [n=7]}, \textbf{data 2 [n=7]}},
            xmin=-22, xmax=22,
            ]   
            
            \addplot[black,thick, bar shift=-1.7 cm, error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt},
            error mark options={
                rotate=90,
                mark size=5pt,
                line width=1pt
            }] 
                plot coordinates {(1, 3.9)+- (1.2,1.2)};
            \addplot[red,thick, bar shift=0 cm, error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt},
            error mark options={
                rotate=90,
                mark size=5pt,
                line width=1pt
            }]  
            plot coordinates {(1, 1.6)+- (0.4,0.4)};
            
        %   \addplot[draw=none]
        %   plot coordinates {(1, 1)};
                    
            \legend{data 1, data 2}
            
            \addplot+[mark=o,only marks,xshift=-1.7cm,black] coordinates
            {(1,4.1) (1,1.2) (1,2.4) (1,13.1) (1,2.5) (1,2.0) (1,1.7)};
            \addplot+[mark=o,only marks,red] coordinates
            {(1,1.1) (1,2.0) (1,3.3) (1,2.5) (1,1.4) (1,2.2) (1,1.2)};
        \end{axis}
    
    \end{tikzpicture}
\end{document}

Graph with two bars and points

1
  • Thank you very much for the input it helped me very much. +1. There was nor particular reason for the plot shift other than to force the plot marks and the box plots together. I got rid of at in my answer.
    – Roland
    Oct 12, 2021 at 8:04
0

Finally, found a solution that works best for me: I removed ybar form the axis enviroment and added xtick= {0,1}, xmin=-1, xmax=2. For the rest see code below.

\documentclass[border=0.5cm]{standalone}

\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            legend style={at={(0.12,0.98)},anchor=north},
            legend cell align={left},
            xticklabels={\textbf{data 1}, \textbf{data 2}},
            x tick label style={rotate=45, anchor=north east, inner sep=1mm},
            ylabel={\textbf{concentration}},
            bar width=1cm,
            grid=none,
            ymin=0,
            ymax=16,
            xtick= {0,1},
            xmin=-1, 
            xmax=2,
            ]
            
            
            \addplot[mark=o,only marks,black] coordinates
            {(0,4.1) (0,1.2) (0,2.4) (0,13.1) (0,2.5) (0,2.0) (0,1.7)};
            \addplot[mark=o,only marks,red] coordinates
            {(1,1.1) (1,2.0) (1,3.3) (1,2.5) (1,1.4) (1,2.2) (1,1.2)};
            
            \addplot[ybar,black,thick,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt},
            error mark options={
                rotate=90,
                mark size=5pt,
                line width=1pt
            }] 
            plot coordinates {(0, 4.2)+- (1.8,1.8)};
            \addplot[ybar,red,thick,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt},
            error mark options={
                rotate=90,
                mark size=5pt,
                line width=1pt
            }]  
            plot coordinates {(1, 1.7)+- (0.3,0.3)};
            
            \legend{data 1, data 2}
            
        \end{axis}

    \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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