18

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}
  • can you please provide a working example? otherwise it is difficult to give a good answer – Martin H Jul 7 '11 at 11:56
  • done. If only I could rotate the text above the bars, it would save a lot of space. – Iain Jul 7 '11 at 12:26
  • 2
    Please always add a full, compilable minimal working example (MWE) that illustrates your problem, not just code fragments. It's just a few lines more (in most cases) and avoids confusion, potential errors and extra works for answerers. – Martin Scharrer Jul 7 '11 at 13:09
18

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}
  • Thats fantastic Jake. Works a treat. Thanks. – Iain Jul 7 '11 at 13:10
  • @Jake How could it be done to rotate the labes D, F, R, M, ... in your sample? – Matthias Jun 15 '15 at 9:11
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.

  • Please reject my edit, it was a mistake – Iain Jul 7 '11 at 12:27
  • I don't see how this helps, nodes near coord automatically generates the text. – Iain Jul 7 '11 at 12:28
  • @Iain: In this case I misunderstood your question. – Martin Scharrer Jul 7 '11 at 12:57
  • @Iain, @Martin: The general idea is definitely correct, the normal TikZ option rotate=90 doesn't work in this case. I've used Martin's approach in my answer. – Jake Jul 7 '11 at 13:01
  • 1
    @Ian: You are welcome. After it turned out that this was useful after all, it would be nice if you take the down-vote back (if its from you). Thanks. – Martin Scharrer Jul 7 '11 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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