Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I create an 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.

share|improve this question
    
I have been wrong before but I think this is not possible as the axis is prepared for either ybar or the ybar stacked. It seems like feature request for a possible mixture of the styles. You can try to place an empty axis on top and shift the bars of the second axis but still it would be a tedious hack. –  percusse Sep 8 '12 at 23:45
    

1 Answer 1

up vote 7 down vote accepted

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

share|improve this answer
    
very good. Its exactly what I was looking for. –  JohnTortugo Sep 9 '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 0:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.