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?







    ybar stacked,
    ymin = 0,
    ymax = 570,
    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)};


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.


        ybar stacked,
        /pgfplots/nodes near coords*/.append style={
            every node near coord/.style={
            scatter/@post marker code/.append code={
        nodes near coords,
        ymin=0,ymax=570,xtick=data,symbolic x coords={a,b},
    \addplot coordinates{(a,   2.9)(b,   2.9)};
    \addplot coordinates{(a,  80  )(b, 200  )};
    \addplot coordinates{(a, 400  )(b, 100  )};
  • 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

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