Take the 2-minute tour ×
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.

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

1 Answer 1

up vote 4 down vote accepted

You can just apply the nodes near coords option to the third dataseries, using \addplot +[nodes near coords] coordinates.... That way, the labels will only be generated for that series.

\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

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.