How can I get multiple stacked plots (ybar stacked, stack plots=y) in one diagram, such that there are multiple stacked bars for the same x-coordinate, each slightly shifted? Is there a way to reset pgfplot's zero levels after an \addplot command, such that the next \addplot[xhshift=...] doesn't add something to the current stack, but rather starts a fresh stack?

FWIW, overlaying two plots with scale only axis won't help; I need this to work with groupplot

1 Answer 1


I assume you want "stacked and clustered bar charts".

The following solution is not the most elegant, but it works quite well. It basically resets the stacked plot variables and starts stacking the following plots. I haven't found a way to properly reset the zero levels for the first plot after the reset command, that's why I use an invisible plot with the forget plot option that has zero values at all coordinates. This does not interrupt the style cycle, but unfortunately it requires hard coding the coordinates and won't work if you don't know the number of bar stacks beforehand.

Plenty of room for improvement, but it's a start and might be enough for you to get the job done.


\addplot [forget plot,draw=none] coordinates{(1,0) (2,0) (3,0)};
    ybar stacked,
    enlarge x limits=0.5,
    legend entries={A,B,C,D,E}
\addplot +[bar shift=-.2cm] coordinates {(1,1) (2,1.75) (3,0.5)};
\addplot +[bar shift=-.2cm] coordinates {(1,0.5) (2,2.5) (3,1.5)};


\addplot  +[bar shift=.2cm]coordinates {(1,1.2) (2,1.5) (3,0.3)};
\addplot  +[bar shift=.2cm] coordinates {(1,0.5) (2,2) (3,1.5)};
\addplot  +[bar shift=.2cm] coordinates {(1,0.2) (2,0.1) (3,0.3)};

  • I've got a follow-up question on this. When you use [xshift] in the addplot` options, a legend created from this plot is also shifted (add a legend entry for your above example to see what I mean). Is there a way to un-shift the legend entry?
    – Trevor
    Commented Aug 18, 2012 at 0:31
  • @Trevor: You're right, thanks for spotting that! I should have used bar shift instead of xshift (the former doesn't influence the legend image). I've edited my answer accordingly.
    – Jake
    Commented Aug 18, 2012 at 8:40
  • Perfect! That did the trick.
    – Trevor
    Commented Aug 18, 2012 at 18:43
  • 1
    one more follow-up... When I use bar shift instead, the bar is in the right place, but the nodes near coords label doesn't shift with it. I'm using your nodes near coords horizontally centered solution as well. Ideas?
    – Trevor
    Commented Aug 19, 2012 at 19:21
  • Is there a way to make this work with symbolic coords? Commented Jan 19, 2018 at 19:59

You must log in to answer this question.