[Tex/LaTex] Stacked bar-plot: display total value

bar chartpgfplots

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:
enter image description here

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 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] 
        \addplot coordinates{(2.68,1) (2.94,0)};
        \addplot coordinates{(26.63,1) (31.67,0)};
        \addplot +[
            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] 
        \addplot coordinates{(2.68,1) (2.94,0)};
        \addplot coordinates{(26.63,1) (31.67,0)};
        \addplot +[
            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}
Related Question