I have the following bar plot.
The code for this is:
\documentclass{standalone}
\usepackage[x11names]{xcolor}
\usepackage{tikz,pgfplots}
\usepackage{graphicx}
\usepackage{ifthen}
\usetikzlibrary{arrows, arrows.meta, bending, shapes, 3d, calc, fit, positioning}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
scale = 0.4,
bar width=15pt,
nodes near coords,
enlargelimits=0.15,
legend style={at={(0.5,-0.20)},
anchor=north},
ylabel={\#bla},
grid,
symbolic x coords={a,b,c,d},
xtick=data,
x tick label style={anchor=north},
]
\addplot+[ybar] plot coordinates {(a,5) (b,15) (c,1) (d,10)};
\addplot+[ybar] plot coordinates {(a,8) (b,29) (c,0) (d,1)};
\legend{\strut blubb, \strut peng}
\end{axis}
\end{tikzpicture}
\end{document}
Obviously several things about this plot are not actually pretty. What I would like to change is:
- Make the numbers at the bars (the blue and red numbers) smaller without making the axis labels (black numbers) smaller, too.
- Control the positioning of the numbers at the bars (blue and red). In particular I do not want them to overlap and to extend over bar-borders such as the 5 on the left does.
- The red numbers always seem to add the two values given in the code. I do not want that. I want the blue number to represent how many 'blubb' I have and the red number who many 'peng' I have.
Can anyone help me with that? Thanks!
Soraltan
Best Answer
I am pretty sure that the following solution addresses the points 1 and 3, but I don't know if I have understood point 2 correctly.
Point 1 can be achieved by adding
and Point 3 is the default since PGFPlots v1.9.
For Point 2 I adapted the solution given here, where now the nodes are placed "above" the bar, if the current bar value is smaller than the given value to the new style, that is in this case
This works fine for the given values, because the small ones are always the last given (or the following are zero where a special handler skips these entries).