Line break in datapoint label with pgfplot?

I have a bar chart, with labels for each column. I would like to break these labels over two lines. I'm not using nodes so the "align" trick doesn't seem to apply.

Minimal example: (from Lyx)

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{ mygraph/.style={ ybar, nodes near coords, every axis plot post/.append style={ point meta=explicit symbolic } }, mygraph/.default={} }

\makeatother
\usepackage{babel}

\begin{document}
\begin{tikzpicture}
\begin{axis}[mygraph,xmax=10,ymax=10,ytick={0,2,...,10}]
\addplot coordinates {(1,5) [Firstword Secondword]}; <= I want a line break here
\end{axis}
\end{tikzpicture}
\end{document}


You should not use \addplot as you are not really doing a plot. Instead use a node as in:

\node [align=left, text width=2cm] at (axis cs:2,6) {Firstword Secondword};


The above acts like a minipage of the specified width and the text is fit in that width. If you prefer to manually specify where the line break should occur then use something like:

\node[align=left] at (axis cs:2,8) {Firstword \\ Secondword};

• Thanks for the fast reply. If I understand right, you mean use \addplot to add the bar, and then manually add the label using \node? Works, but would be great if there was an automatic way of doing it. – moabi2000 Oct 11 '11 at 16:44
• @moabi2000: Yes, \addplot is for plotting, and adding a text label is not really plotting. I am not sure what you mean by automated. Perhaps you should post a new question detailing the automation you desire and how you would want to specify it in a MWE. – Peter Grill Oct 11 '11 at 16:50
• What I mean is that using my example above, the labels are (if they are on one line) automatically placed just above the appropriate bar. If I rescale the axis (changing ymin for example), or I correct the datapoint, the text is automatically moved also into its new position. Using \node, I have to calculate manually where to put it based on the current text size and y-scale. If I modify the y-scale, or the data value I have to re-calculate and update the coordinates for the node. So not very scalable (in terms of effort) for making many plots with slightly different data. – moabi2000 Oct 11 '11 at 17:30
• Ok, solved. Can't answer my own question yet, but the solution is to add: "every node near coord/.append style={align=center, text width=1cm}" in the preamble. – moabi2000 Oct 11 '11 at 18:49

Ok, worked it out based on this question and answer.

every node near coord/.append style={
align=center,
text width=1cm
}


To the preamble. My test document now looks like this:

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\makeatletter
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{ mygraph/.style={ ybar, nodes near coords, every node near coord/.append style={
align=center,
text width=1cm
},
every axis plot post/.append style={ point meta=explicit symbolic }
}, mygraph/.default={} }

\makeatother
\usepackage{babel}

\begin{document}
\begin{tikzpicture}
\begin{axis}[mygraph,xmax=10,ymin=4,ymax=10,ytick={0,2,...,10}]