Is there a way to make pgfplots
display only the total value of a stacked bar-plot?
With nodes near coords
, all segment-values are shown:
I'm only interested in the sum of the stacks, though. Basically, the last brown-ish value printed in black would be perfect.
Here's the MWE which generates the picture above:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{testbar/.style={
xbar stacked,
width=.8\textwidth,
axis y line*= none, axis x line*= bottom,
xmajorgrids = true,
xmin=0,xmax=70,
ytick = data, yticklabels = {C1,C2},
tick align = outside, xtick pos = left,
bar width=6mm, y=8mm,
enlarge y limits={abs=0.625},% 0.5 + 0.5*(y - bar width)/y [TeX.sx #47995]
nodes near coords,
nodes near coords align={horizontal},
}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[testbar]
\addplot coordinates{(2.68,1) (2.94,0)};
\addplot coordinates{(26.63,1) (31.67,0)};
\addplot coordinates{(6.2,1) (9.91,0)};
\end{axis}
\end{tikzpicture}
\end{document}
Best Answer
To only generate one label per bar stack, add the
nodes near coords
option to the last series only, using\addplot +[nodes near coords] coordinates...
.In order to place the labels at the end of the bars, and not in the middle of the bar, starting with PGFPlots version 1.9 you have to add
nodes near coords xbar stacked configuration/.style={}
before callingxbar stacked
.To print the sum of the bar stacks, instead of the value of the last bar part, starting with PGFPlots version 1.9 you have to add
point meta=x
to your\addplot
options.Code for PGFPlots >= 1.9:
Code for PGFPlots < 1.9: