1

I have several ybar stacked plots and I would like to put them together in one plot.

However, since the ybars of the different plots share the same coordinates, when I put them in one plot, they just stack up into a single ybar. However, I would like to have the original stacked ybars be displayed side by side at the given coordinate (just as non-stacked ybars do).

Anybody got an idea how to do that?

  • Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Max Aug 21 '18 at 14:33
1

It is very simple: use two axis environments and shift the bars a bit. If you are very picky, you need to change the unit vector ratio of one of the plots a bit.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
    width=8cm,
    unit vector ratio=1 0.5, 
    symbolic x coords={a,b,c,d,e},
    xtick=data,
    ybar stacked,
    ytick pos=left,
    ]
        \addplot[ybar,fill=blue!30,mark=no,bar shift=-3mm] coordinates {
         (a,3.5)
         (b,0.75)
         (c,4.85)
         (d,3.33)
         (e,7)
        };
        \addplot[ybar,fill=orange,mark=no,bar shift=-3mm] coordinates {
         (a,2.5)
         (b,1.75)
         (c,1.85)
         (d,2.33)
         (e,1)
        };
    \end{axis}
    \begin{axis}[ytick=\empty,
    width=8cm,
    unit vector ratio=1 0.5, 
    symbolic x coords={a,b,c,d,e},
    xtick=data,
    ybar stacked,
    ytick pos=left,
    ]
        \addplot[ybar,fill=green!30,mark=no,bar shift=3mm] coordinates {
         (a,2.5)
         (b,1.75)
         (c,1.85)
         (d,2.33)
         (e,1)
        };
        \addplot[ybar,fill=red,mark=no,bar shift=3mm] coordinates {
         (a,3.5)
         (b,0.75)
         (c,4.85)
         (d,3.33)
         (e,7)
        };
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

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.