# Stacked bar-plot: display total value

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]
\end{axis}
\end{tikzpicture}

\end{document}


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 calling xbar 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:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\pgfplotsset{testbar/.style={
nodes near coords xbar stacked configuration/.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]
}}

\begin{document}

\begin{tikzpicture}
\begin{axis}[testbar]
point meta=x,
nodes near coords,
nodes near coords align={anchor=west},
every node near coord/.append style={
black,
fill=white,
fill opacity=0.75,
text opacity=1,
outer sep=\pgflinewidth % so the label fill doesn't overlap the plot
}
]coordinates{(6.2,1) (9.91,0)};
\end{axis}
\end{tikzpicture}

\end{document}


Code for PGFPlots < 1.9:

\documentclass[border=5mm]{standalone}
\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]
}}

\begin{document}

\begin{tikzpicture}
\begin{axis}[testbar]
nodes near coords,
nodes near coords align={horizontal},
every node near coord/.append style={
black,
fill=white,
fill opacity=0.75,
text opacity=1,
outer sep=\pgflinewidth % so the label fill doesn't overlap the plot
}
]coordinates{(6.2,1) (9.91,0)};
\end{axis}
\end{tikzpicture}

\end{document}

• @Jake I know this is a really old Q/A, but your solution doesn't seem to work anymore. Any idea either what "compat" your solution uses, or how to do this with a newer pgfplots? – crockeea May 19 '16 at 4:07
• @Eric: The behaviour of the nodes near coords for stacked bars was improved in PGFPlots 1.9, but for this use case (total value of stack printed at the end of the stack) some additional keys are necessary. I've updated my answer. Thanks for bringing this to my attention! – Jake May 19 '16 at 9:00
• How to get this chart vertical? – bikashg Jan 18 '19 at 16:54