2

I am trying to create a bar chart with both positive and negative stacked bars. However, my negative bar doesn't appear/it is hidden behind the positive bars. How should I change the settings to make the positive bars stacked and make the negative bar appear below them?

Here's a MWE:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar stacked,xtick=\empty,ytick=\empty]
\addplot+[fill=red,draw=black] coordinates {(0,1) (.5,1.5) (1,2)};
\addplot+[fill=blue,draw=black] coordinates {(0,.5) (.5,1) (1,.5)};
\addplot+[fill=yellow,draw=black] coordinates {(0,-.5) (.5,-1) (1,-.5)};
\legend{Income1,Income2,Investment}
\end{axis}
\end{tikzpicture}
\end{document}

Which generates:

Result

  • @ChristianFeuersänger may I ping you to this post? Looks a bit odd. I hope I did not miss something. Thank you. – LaRiFaRi Dec 15 '14 at 16:18
1

I would report that to the maintainers. Looks really strange. If you do not stack them, one gets the correct result.

% arara: pdflatex

\documentclass[tikz,border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[ybar,xtick=\empty,ytick=\empty,legend cell align=left, legend style={legend pos=north west}]
    \addplot+[fill=yellow,draw=black] coordinates {(0,-.5) (.5,-1) (1,-.5)};
    \addplot+[fill=red,draw=black] coordinates {(0,1) (.5,1.5) (1,2)};
    \addplot+[fill=blue,draw=black] coordinates {(0,.5) (.5,1) (1,.5)};
    \legend{Income1,Income2,Investment}
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

  • I'm glad you understand the question. If we could stack the first two bars and then plot the negative bar in the same axis but without appending it to the existing bars, the problem would be solved – Grimler Dec 15 '14 at 16:34
1

I solved it by using groupplots and some manipulations.

Code:

\documentclass[]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size= 1 by 2},height=5cm,width=6.4cm,xtick=\empty]%[ybar stacked,xtick=\empty,]%ytick=\empty]
\nextgroupplot[ybar stacked,xtick=\empty,ymin=0,ymax=3,legend style = {at={(1.05, 0.5)}, anchor = west, legend columns =1, draw=none, area legend},  ymajorgrids = true,]%ytick=\empty]
\addplot[ybar stacked,fill=red,draw=black] 
coordinates {(0,1) (.5,1.5) (1,2)};
\addplot[ybar stacked,fill=blue,draw=black] 
coordinates {(0,.5) (.5,1) (1,.5)};
\addlegendentry{Income1}
\addlegendentry{Income2}
\nextgroupplot[yshift=1cm,ymin=-1.5,ymax=0,height=3.2cm,ytick=\empty,extra y ticks={-1},legend style = {at={(1.05, 1.65)}, anchor = west, legend columns =1, draw=none, area    legend},ymajorgrids = true,]
\addplot[ybar stacked,fill=yellow,draw=black] coordinates {(0,-.5) (.5,-1) (1,-.5)};
\addlegendentry{Investment}
\end{groupplot}
\end{tikzpicture}
\end{document}

Which generates: 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.