1

I am struggling with creating a stacked bar chart and I am not really sure why it is not working. Attached a minimal working example.

I have 2 problems. a) One bar should fill the range 0-30, the second one 0-70 and I want them to overlap. It seems to add up the values which means that it is 30+70 and the second bar thus extends until 100.

b) I defined several bars ranging from a to g, but for unknown reason a and b are swallowed and the plot only uses c to g.

Sorry for the questions, but I spend some time comparing my code to stacked bar examples and I just don't see what my problem is here. Does anyone see what the issue is?

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}    

\begin{document}

\begin{figure}
\begin{tikzpicture}
  \begin{axis}[
        xmin=60,  
        xmax=100,
        xbar stacked,
        xmajorgrids = true,
        bar width=1mm, 
        width=11.5cm,
        height=10cm, 
        xlabel={Accuracy \%},
        symbolic y coords={a,b,c,d,e,f,g},
        ytick=data
  ]


\addplot[fill=orange] coordinates {(50.5,a)(40.3,b)(30.0,c)(30.0,d)(30.0,e)(30.0,f)(30.0,g)};
\addplot[fill=black] coordinates {(70.7,a)(70.2,b)(70,c)(70,d)(70,e)(70,f)(70,g)};

\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}
0

2 Answers 2

3

The [xbar stacked] option means the add the next bar to the right of the first (starting at xmin). Actually, your biggest problem was [xmin=60] which cut out all the orange bars.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}    

\begin{document}

\begin{figure}
\begin{tikzpicture}
  \begin{axis}[
        xmin=0,  
        xmax=100,
        xbar=-1mm,
        xmajorgrids = true,
        bar width=1mm, 
        width=11.5cm,
        height=10cm, 
        xlabel={Accuracy \%},
        symbolic y coords={a,b,c,d,e,f,g},
        ytick=data
  ]

\addplot[fill=black] coordinates {(70.7,a)(70.2,b)(70,c)(70,d)(70,e)(70,f)(70,g)};
\addplot[fill=orange] coordinates {(50.5,a)(40.3,b)(30.0,c)(30.0,d)(30.0,e)(30.0,f)(30.0,g)};

\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

demo

0

I think you should begin by drawing the black plot :

But I didn't see the other issue.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}    

\begin{document}

\begin{figure}
\begin{tikzpicture}
  \begin{axis}[
        xmin=60,  
        xmax=100,
        xbar stacked,
        xmajorgrids = true,
        bar width=1mm, 
        width=11.5cm,
        height=10cm, 
        xlabel={Accuracy \%},
        symbolic y coords={a,b,c,d,e,f,g},
        ytick=data
  ]


\addplot[fill=black] coordinates {(70.7,a)(70.2,b)(70,c)(70,d)(70,e)(70,f)(70,g)};

\addplot[fill=orange] coordinates {(50.5,a)(40.3,b)(30.0,c)(30.0,d)(30.0,e)(30.0,f)(30.0,g)};

\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .