20

I'm plotting a ybar, and nodes near coords is ugly. Is it possible to rotate the text above the bars by 90?

\begin{tikzpicture}
\begin{axis}[area legend,
legend pos=north west,
legend columns=4,
legend style={draw=none},
symbolic x coords={D,F,R,M,K,S,C},
major tick length=0cm,
xtick=data,
nodes near coords,
ybar,
bar width = 6pt,
]
\addplot[area legend] %[ybar,,fill=blue]  fill=color1
coordinates {(D,0.78) (F,0.23) (R,0.93) (M,0.73) (K,0.00) (S,0.37) (C,0.36)};
\addplot[area legend] %[ybar,,fill=blue]
coordinates {(D,0.52) (F,0.62) (R,0.60) (M,0.56) (K,0.64) (S,0.166) (C,0.170)};
\addplot[area legend] %[ybar,,fill=blue]
coordinates {(D,0.635) (F,0.437) (R,0.744) (M,0.345) (K,0.48) (S,0.652) (C,0.955)};
\end{axis}
\end{tikzpicture}
Stefan Pinnow
  • 29,535
Iain
  • 303

2 Answers2

19

You can rotate the nodes using

every node near coord/.append style={rotate=90, anchor=west}

Note that this option needs to be added after ybar, since ybar overwrites the every node near coord style.

The nodes near coords do not automatically enlarge the plot area to accommodate them, so you have to do something like enlarge y limits={upper,value=0.2} to adjust the top border of the plot area.

(Unrequested extra information: The option area legend has to be passed to the addplots, it is not an axis option. To do this for all plots, you have to use the axis option every axis plot post/.style={area legend})

pgfplots with rotated labels

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    symbolic x coords={D,F,R,M,K,S,C},
    major tick length=0cm,
    xtick=data,
    enlarge y limits={upper,value=0.2},
    nodes near coords,
    ybar,
    every node near coord/.append style={rotate=90, anchor=west},
    bar width = 6pt,
]
\addplot
coordinates {(D,0.78) (F,0.23) (R,0.93) (M,0.73) (K,0.00) (S,0.37) (C,0.36)};
\addplot
coordinates {(D,0.52) (F,0.62) (R,0.60) (M,0.56) (K,0.64) (S,0.166) (C,0.170)};
\addplot
coordinates {(D,0.635) (F,0.437) (R,0.744) (M,0.345) (K,0.48) (S,0.652) (C,0.955)};
\end{axis}
\end{tikzpicture}
\end{document}
Jake
  • 232,450
1

You can use \rotatebox{<angle>}{Text} (from the already loaded graphicx) package to rotate text. More complicated stuff like verbatim text can be rotated using the {adjustbox}{angle=<angle>} environment provided by the adjustbox package. Simply use these macros inside the node, but TikZ might have own options for it.

Martin Scharrer
  • 262,582