7

How can I create a pgfplot that have two stacked ybar for each "point in x axis" ?

I wan't something like this:

enter image description here

I already have a graph that have one stacked bar for each x point. Some hint on how to extend it or a link for an example will be sufficient.

1 Answers1

9

I had to try, but I think in the end I agree with percusse: It's a littletedious. You can use two axis and use the (so far quite convineant) bar shift=<lenth> option, however, depending on the width of the picture, you'll have to experiment a little. Furthermore, you have to use the ymin=<number> and ymax=<number> as otherwise the y-axes won't align properly. Here's an example to illustrate it cane be done with a little hassle:

Code

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{width=10cm}

\begin{document}

\begin{tikzpicture}
\begin{axis}[ybar stacked,bar shift=7pt,ymin=0,ymax=11,symbolic x coords={A,B,C,D,E}]]  
\addplot coordinates
{(A,1) (B,1) (C,3) (D,2) (E,1.5)};
\addplot coordinates
{(A,1) (B,1) (C,3) (D,2) (E,1.5)};
\addplot coordinates
{(A,1) (B,1) (C,3) (D,2) (E,1.5)};
\end{axis}
\begin{axis}[ybar stacked,bar shift=-7pt,ymin=0,ymax=11,symbolic x coords={A,B,C,D,E}]  
\addplot+[fill=blue!50!gray] coordinates
{(A,1.5) (B,1.5) (C,3.5) (D,2.5) (E,1)};
\addplot+[fill=red!50!gray] coordinates
{(A,1.5) (B,1.5) (C,3.5) (D,2.5) (E,1)};
\addplot+[fill=yellow!50!gray] coordinates
{(A,1.5) (B,1.5) (C,3.5) (D,2.5) (E,1)};
\end{axis}

\end{tikzpicture}

\end{document}

Result

enter image description here

Tom Bombadil
  • 40,123
  • very good. Its exactly what I was looking for. – JohnTortugo Sep 09 '12 at 13:45
  • @JohnTortugo if Tom has solved the problem, you can reward him by accepting his answer using the green check mark- this will give him 15 well-earned reputation points :) – cmhughes Sep 10 '12 at 00:21