TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}
share|improve this question
up vote 6 down vote accepted

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}
share|improve this answer
    
Perfect answer. Thanks, Jake! – dhst Mar 23 '12 at 6:22
    
@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? – Eric May 19 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 at 9:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.