Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
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

2 Answers

up vote 6 down vote accepted

The node text can be specified using the optional argument to nodes near coords. The standard content is \pgfmathprintnumber\pgfplotspointmeta, i.e. the meta value (usually the y value) is printed by the math output routine. You can rotate the labels by using the option nodes near coords=\rotatebox{90}{\pgfmathprintnumber\pgfplotspointmeta}, as Martin suggested.

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=\rotatebox{90}{\pgfmathprintnumber\pgfplotspointmeta},
    ybar,
    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}

(Extra extra information:) You can adjust the style that is used for the nodes near coords nodes using nodes near coords align={<options>}, although it is typically used with options such as horizontal, vertical or <anchor> to easily adjust the placement. If you pass the option rotate=90, the nodes do get rotated, but because PGFplots falls back to very simple node placement if it encounters arbitrary options in nodes near coords align, the nodes will be placed at the same horizontal position in the case of ybar plots.

share|improve this answer
Thats fantastic Jake. Works a treat. Thanks. – Iain Jul 7 '11 at 13:10
@Iain: Glad to hear that! If you consider your question answered, you can click the tick mark next to the answer to mark the question as answered. – Jake Jul 7 '11 at 13:11

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.

share|improve this answer
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
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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