11

I'm trying to produce a bar chart for publication with math text in the tick labels. The most readable layout would be to angle them, but when I do they clash with the axis and are difficult to relate to the correct bar. I've tried padding with \quad spaces, but this pushes them even further from the bar they are supposed to be labelling. Is there a way of offsetting the label positions, or some more elegant solution?

Minimal example:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}

\begin{axis}[%
xmin=0.5, xmax=5.5,
xtick={1,2,3,4,5},
xticklabels={
$\textrm{fish}^\textrm{cake}$,$\Delta \textrm{bob}^2$,spoon,$e^{i \pi}+1 = 0$,knife},
x tick label style={rotate=45, anchor=east},
ylabel={Energy / Hadouken mol$^{-1}$}]
\addplot[ybar,bar width=0.6cm,fill=gray,draw=black] plot coordinates{(1,10)
(2,20)
(3,40)
(4,-20)
(5,10)};


%% This is just to add a zero line for clarity
\addplot [
color=black,
solid,
forget plot
]
table{
0 0
6 0
};

\end{axis}
\end{tikzpicture}
\end{document}

Output:

enter image description here

13

I can think of two things:

  • Anchor the labels at the north east rather than the east. Northeast makes more sense when they are placed diagonally in this way.

  • Reduce the spacing around the labels by setting inner sep=0mm.

Result:

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}

\begin{axis}[%
xmin=0.5, xmax=5.5,
xtick={1,2,3,4,5},
xticklabels={
$\textrm{fish}^\textrm{cake}$,$\Delta \textrm{bob}^2$,spoon,$e^{i \pi}+1 = 0$,knife},
x tick label style={rotate=45, anchor=north east, inner sep=0mm},
ylabel={Energy / Hadouken mol$^{-1}$}]
\addplot[ybar,bar width=0.6cm,fill=gray,draw=black] plot coordinates{(1,10)
(2,20)
(3,40)
(4,-20)
(5,10)};


%% This is just to add a zero line for clarity
\addplot [
color=black,
solid,
forget plot
]
table{
0 0
6 0
};

\end{axis}
\end{tikzpicture}
\end{document}
  • That's great. Between those two parameters there should be enough control for most things. Cheers! – Adam J May 7 '13 at 17:55

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.