ybar stacked: Position of labels

I want to display some results of a performance test in a diagram as ybar stacked, but as you can see, some labels are printed over others and are hard to read (because of colors). Now I want labels on right side of bars, like here, but is there an automatic way to do this or I have to create all labels on my own? And can I make it so that lines are not going horizontal, and targeting the center of the stack element?

Current:

Wished:

\documentclass[chapterprefix=true]{scrbook}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepackage{tikz}
\usetikzlibrary{pgfplots.statistics}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ybar stacked,
ymin = 0,
ymax = 570,
xtick=data,
symbolic x coords={a, b},
nodes near coords]
\addplot coordinates{ (a, 2.9) (b, 2.9)};
\addplot coordinates{ (a, 80) (b, 200)};
\addplot coordinates{ (a, 400) (b, 100)};
\end{axis}
\end{tikzpicture}

\end{document}


This is a relatively short way to do so. Here is how it works:

The name=X gives the label a (temporary) name, (X). And then I shift it so it is no longer overlapping bars.

On the other hand I need a reference to the original position. So I .append code with a node. You can search node near coord in pgfplots.code.tex to see where the former is appended. The trick is that the declaration of (Y) is following (X) so (Y) would be at the original position that the label should be.

Finally draw the line. Now you can modify the style yourself using bend left or something like \draw[fill=none](Y.center)--+(10pt,0pt)--+(15pt,20pt)--(X);. Notice that .center is necessary.

\documentclass{article}
\usepackage{pgfplots}\pgfplotsset{compat=1.12}\usetikzlibrary{pgfplots.statistics}

\begin{document}
\makeatletter
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
/pgfplots/nodes near coords*/.append style={
every node near coord/.style={
name=X,
shift={(30pt,20pt)}
},
scatter/@post marker code/.append code={
\node(Y){};
\draw(X)--(Y.center);
}
},
nodes near coords,
ymin=0,ymax=570,xtick=data,symbolic x coords={a,b},
]
\addplot coordinates{(a,  80  )(b, 200  )};
\addplot coordinates{(a, 400  )(b, 100  )};
\end{axis}
\end{tikzpicture}
\end{document}

• Nice solution :). Is there a way to prevent automatically overlapping? E.g. if you have \addplot coordinates{(a, 2.9)(b, 2.9)}; \addplot coordinates{(a, 2.9)(b, 2.9)}; \addplot coordinates{(a, 80 )(b, 200 )}; \addplot coordinates{(a, 400 )(b, 100 )}; – SunBlack Mar 11 '15 at 21:55
• I guess you mean the following: (1) If the current subbar is high enough, put the label inside (2) otherwise use a call out node. For the former it is easy to do such checking. For the latter it would be enough to have shift={(20pt,2*\coordinates@count*\text@size)}. But I doubt whether such counter exists. – Symbol 1 Mar 12 '15 at 0:38
• Oh, by the way, you should edit your question so people will easily understand what is going on. – Symbol 1 Mar 12 '15 at 0:47

protected by karlkoellerMar 15 '15 at 12:47

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).