5

I have a figure where one of my labels is very long. I would like this label to be split to two rows. Currently my code is like this

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{width=10 cm}
\begin{axis} [
symbolic x coords={0, Label 1, Label 2, This is a very very very long label, Label 4, Label 5, Label 6, Label 7,Label 8},
xtick={Label 1, Label 2, This is a very very very long label, Label 4, Label 5, Label 6, Label 7},
x tick label style={rotate=45, anchor=east, align=center},
axis lines*=left,
ymajorgrids = true,
legend style={at={(0.5,-0.30)},anchor=north},
ymin=0,
ymax=175,
bar width=5mm,
ybar=-0.5cm, 
enlarge x limits={abs=0.6cm},
nodes near coords,        
every node near coord/.append style={color=black},
]
\addplot [red,fill=red] 
coordinates{ (Label 1,100) (Label 2,90) (This is a very very very long label,80) (Label 4,140) } ;
\addplot [blue,fill=blue]
coordinates{ (Label 5,80) (Label 6,25) (Label 7,66) } ;
\addlegendentry{Legend 1}
\addlegendentry{Legend 2}
\end{axis}
\end{tikzpicture}

\end{document}

Which produces this: enter image description here

Now if I try to change the x tick label style to be

x tick label style={rotate=45, anchor=east, align=center,text width=3.5cm}

This happens: enter image description here

So the anchor=east command doesn't seem to go along with the text width command. Other anchor commands like anchor=west, anchor=base seem to be working perfectly normal. Does anyone know why the anchor east command doesn't seem to work with the text width command and is there a way to go around this?

8

Your x tick labels are anchored to the east. But since you are giving align=center and textwidth=3.5cm the labels that are shorter will be aligned to the center with blank space to the right. Hence they stay bit away from the x-axis. To solve it align them to the right with align=right in

x tick label style={rotate=45, anchor=east, align=right,text width=3.5cm},

Full code:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{width=10 cm}
\begin{axis} [
symbolic x coords={0, Label 1, Label 2, This is a very very very long label, Label 4, Label 5, Label 6, Label 7,Label 8},
xtick={Label 1, Label 2, This is a very very very long label, Label 4, Label 5, Label 6, Label 7},
x tick label style={rotate=45, anchor=east, align=right,text width=3.5cm},
axis lines*=left,
ymajorgrids = true,
legend style={at={(0.5,-0.30)},anchor=north},
ymin=0,
ymax=175,
bar width=5mm,
ybar=-0.5cm,
enlarge x limits={abs=0.6cm},
nodes near coords,
every node near coord/.append style={color=black},
]
\addplot [red,fill=red]
coordinates{ (Label 1,100) (Label 2,90) (This is a very very very long label,80) (Label 4,140) } ;
\addplot [blue,fill=blue]
coordinates{ (Label 5,80) (Label 6,25) (Label 7,66) } ;
\addlegendentry{Legend 1}
\addlegendentry{Legend 2}
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

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.