5

I'm trying to have a stacked bar chart work like a pie chart, with the percentage and label appear in each rectangle. I can get the values appear with a percentage sign, but not the actual text. Maybe there's something else that needs to go as the argument to nodes near coords, or some other way entirely. Here's my mwe:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=1.19\linewidth, axis lines=none, xbar stacked, nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}]
\addplot+[black,fill=white,xbar] plot coordinates {(10,0) [Homework]};
\addplot+[black,fill=white,xbar] plot coordinates {(10,0) [Quizzes]};
\addplot+[black,fill=white,xbar] plot coordinates {(20,0) [Midterm 1]};
\addplot+[black,fill=white,xbar] plot coordinates {(20,0) [Midterm 2]};
\addplot+[black,fill=white,xbar] plot coordinates {(40,0) [Final]};
\end{axis}
\end{tikzpicture}
\end{document}

I want the text labels ("Homework", "Quizzes", etc) to appear inside or below each rectangle, perhaps rotated by some angle so that they fit in the available width.

ronno
  • 1,325

1 Answers1

4

By default \pgpflotspointmeta for an xbar is, as you've seen, the x-value. To access the text you've added, you need to set point meta=explicit symbolic, which means that pgfplots will use whatever you have explicitly written in the coordinate list as the meta value, and that it is not interpreted as a number (that's the symbolic part).

To access the x-value in addition, you can use visualization depends on, as in this example:

output of the code below

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  % needed to increase width a bit
  width=1.4\linewidth,
  axis lines=none,
  xbar stacked,
  % increased bar width to make room for text
  bar width=30pt,
  point meta=explicit symbolic,
  visualization depends on=x \as \XVal,
  nodes near coords={%
    \pgfmathprintnumber\XVal\% \\
    \tiny\pgfplotspointmeta
  },
  every node near coord/.style={
      % allows for linebreaks
      align=center
  } 
]
\addplot+[black,fill=white] coordinates {(10,0) [Homework]};
\addplot+[black,fill=white] coordinates {(10,0) [Quizzes]};
\addplot+[black,fill=white] coordinates {(20,0) [Midterm 1]};
\addplot+[black,fill=white] coordinates {(20,0) [Midterm 2]};
\addplot+[black,fill=white] coordinates {(40,0) [Final]};
\end{axis}
\end{tikzpicture}
\end{document}

That's perhaps not ideal though, and it's also not that flexible (I couldn't find a way of storing the meta-value in a macro, as below). In the following example I changed the way coordinates are input, simply because that does make things easier, and more flexible

Note: You don't need the plot keyword when using \addplot. That is, use \addplot +[..] coordinates, not \addplot +[..] plot coordinates. And you don't need to specify xbar for each plot when you have xbar stacked in the axis options.

output of code

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  width=1.19\linewidth,
  axis lines=none, 
  xbar stacked,
  % save number from first table column in macro \XVal
  visualization depends on=\thisrowno{0} \as \XVal,
  % save number from third table column in macro \MetaVal
  % "value" means that it is _not_ parsed as a number
  visualization depends on=value \thisrowno{2} \as \MetaVal,
  % use \XVal in nodes near coords
  nodes near coords={\pgfmathprintnumber{\XVal}\%},
  % add a label to print the meta info
  every node near coord/.style={
    label={[anchor=west,rotate=-45]below:\MetaVal}
  }
]

\addplot +[black,fill=white] table[header=false,col sep=comma] {
10, 0, Homework
};
\addplot +[black,fill=white] table[header=false,col sep=comma] {
10, 0, Quizzes
};
\addplot +[black,fill=white] table[header=false,col sep=comma] {
20, 0, Midterm 1
};
\addplot +[black,fill=white] table[header=false,col sep=comma] {
20, 0, Midterm 2
};
\addplot +[black,fill=white] table[header=false,col sep=comma] {
40, 0, Final
};

\end{axis}
\end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688