1

I'm trying to make a little stacked bar graph because everyone knows stacked bar graphs are the only type of bar graph (kidding).

I'm trying to make the bar graph represent a y-axis of 0 to 100 properly, so I'm not exactly sure how to start the axis at 0.

Secondly, I want to add number labels that are both properly placed and correctly colored. Currently (as depicted in the photo below), I'm only seeing a label for the top one (.9%) when I'm looking for percentage labels on each. Additionally, I would like to make it so that the label is black for the lighter sections of the plot, and white for the darker sections of the plot.

Finally, and these may very well be tied with the other problems, but I'm getting a few error messages that I wouldn't know how to fix:

  1. Package color Error: Argument `-1' not in range [0,1].
  2. Package color Error: Argument `-0.5' not in range [0,1].
  3. Package pgfplots Warning: Axis range for axis x is approximately empty; enlarging it (it is [0.0:0.0])

Please help and thank you!

Here's a sample of my code:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ybar stacked,
bar width=45pt,
nodes near coords,
enlargelimits=0,
legend style={at={(.855,1)},
  anchor=north},
ylabel={Percentage},
xtick=\empty,
]
\addplot+[ybar] plot [style={green,fill=green}] coordinates {(0,60)};
\addplot+[ybar] plot [style={orange,fill=orange}] coordinates {(0,17)};
\addplot+[ybar] plot [style={yellow,fill=yellow}] coordinates {(0,6)};
\addplot+[ybar] plot [style={blue,fill=blue}] coordinates {(0,6)};
\addplot+[ybar] plot [style={purple!200,fill=purple!200}] coordinates {(0,5.1)};
\addplot+[ybar] plot [style={black,fill=black}] coordinates {(0,5)};
\addplot+[ybar] plot [style={black,fill=white}] coordinates {(0,.9)};
\legend{Doramus,Refaltido,Misanit,Fanatha,Sonphay,Laoli,Tidanme}
\end{axis}
\end{tikzpicture}

\end{document}

Here's what I'm seeing when I compile:

Look at that weird graph!

  • Ok, I also feel like something should be addressed: Please refrain from giving your opinions on stacked bar graphs and whether or not you like them or think they do or do not work. Just trying to work this out. :) – The_Poseidolon Jan 15 '18 at 9:14
  • 1
    I can't remember seeing much negative comments on stacked bar charts. Pie charts on the other hand ... ymin=0. Regarding the errors, purple!200 means mix 200 percent purple with (I think) white, which isn't really possible. Use just purple. The third of those is a warning, not an error, you can safely ignore it I'd say. – Torbjørn T. Jan 15 '18 at 9:23
2

First things first: you can use xmin, xmax, ymin and ymax to set the axis limits, so adding ymin=0 to the axis options will take care of your first concern.

As for the numbers, your bars are actually all numbered properly, you just can't see it, because when you add just a colour name as a key, e.g. green, that also sets the text colour to green. It might make more sense to do e.g. green, text=black, which will set both fill and draw to green, but the text will be black.

Regarding your errors, when you do purple!200, you're asking for 200% purple, which isn't really possible. The values you use for color mixes should be no higher than 100. Remove !200 and the first two errors go away.

The third one is just a warning, not an error. I think you can ignore it. It's just saying that the x-coordinates you've used for your plots span a quite small range (from zero to zero), so pgfplots have expanded the range a bit.

Finally, a little comment about the syntax \addplot+[ybar] plot [style={green,fill=green}] coordinates {(0,60)};. While it works, you're actually mixing the syntax from plain TikZ (\draw plot ..) with the syntax from pgfplots (\addplot ..) which I find a bit odd. You also don't need to specify ybar for each plot when you have ybar stacked in the axis options. Taking into account the text colour as mentioned above, I think it would make more sense to do

\addplot +[green,text=black] coordinates {(0,60)};

A complete example:

output of code

\documentclass{article}
\usepackage{pgfplots} % loads tikz
\pgfplotsset{compat=1.9}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  ybar stacked,
  bar width=45pt,
  nodes near coords,
  enlargelimits=0,
  legend style={
    at={(.855,1)},
    anchor=north
  },
  ylabel={Percentage},
  xtick=\empty,
  ymin=0 % <------added
]
\addplot +[green,text=black] coordinates {(0,60)};
\addplot +[orange,text=black] coordinates {(0,17)};
\addplot +[yellow,text=black] coordinates {(0,6)};
\addplot +[blue,text=white] coordinates {(0,6)};
\addplot +[purple,text=white] coordinates {(0,5.1)};
\addplot +[black,text=white] coordinates {(0,5)};
\addplot +[draw=black,fill=white] coordinates {(0,.9)};

\legend{Doramus,Refaltido,Misanit,Fanatha,Sonphay,Laoli,Tidanme}
\end{axis}
\end{tikzpicture}
\end{document}
  • Thanks so much! Very helpful. In this example, is there a way to offset the numbers (primarily looking at the top one which is halfway through the graph)? – The_Poseidolon Jan 15 '18 at 16:27
  • @The_Poseidolon Basically see tex.stackexchange.com/a/221018, e.g. \addplot +[draw=black, fill=white, every node near coord/.append style={yshift=5pt}] coordinates {(0,.9)}; – Torbjørn T. Jan 15 '18 at 17:44

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.