# How to put data labels next to the text labels in a bar plot? (2nd edition)

I had asked an earlier question How to put data labels next to the text labels in a bar plot?. While the solution provided worked for the specific input data values in that example, when I tried to use adapt the solution for different input data values, I get errors.

When I try to compile the following code:

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ytick={1,2,3},
yticklabels={Optimized Prices,Current Prices,
No Promotions},
xbar,
xlabel=Profit,
nodes near coords, nodes near coords align={horizontal},
% begin new bit
visualization depends on=x \as \rawx,
every node near coord/.append style={
shift={(axis direction cs:-\rawx+0.8,0)}}
% end new bit
]
x y
1000 3
1100 2
1200 1
};
\end{axis}
\end{tikzpicture}
\end{document}


I get the error

! Package PGF Math Error: Could not parse input '-1000.0000000+0.8' as a floati
ng point number, sorry. The unreadable part was near '+0.8'..

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
...

l.26     \end{axis}


If I delete the +0.8, and compile the following code:

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ytick={1,2,3},
yticklabels={Optimized Prices,Current Prices,
No Promotions},
xbar,
xlabel=Profit,
nodes near coords, nodes near coords align={horizontal},
% begin new bit
visualization depends on=x \as \rawx,
every node near coord/.append style={
shift={(axis direction cs:-\rawx,0)}}
% end new bit
]
x y
1000 3
1100 2
1200 1
};
\end{axis}
\end{tikzpicture}
\end{document}


I get the output below:

What is wrong with the above code? I understand that I have shifted the text labels by the wrong length, but how do I specify the right length?

-
The 0.8 is a little small- it worked well in the previous example because of the scaling. If you make it more in line with the current scaling (e.g 800), it might get better. Perhaps one of the gurus has a robust general method that trumps my somewhat-manual approach.... –  cmhughes Nov 7 '13 at 20:03

When you do shift={(axis direction cs:-\rawx,0)} you shift the node to x = 0. However, in your case the axis starts at about x = 980, so the nodes end up 980 axis units left of the axis. The shift you need to do is xmin - \rawx, which will be the length of the individual xbar. You can access xmin with \pgfkeysvalueof{/pgfplots/xmin}, so the needed shift is

shift={(axis direction cs:\pgfkeysvalueof{/pgfplots/xmin}-\rawx,0)}}


(The first bar is too short to fit the text, you could for example set xmin=900 to make them a bit longer.)

\documentclass{standalone}

\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ytick={1,2,3},
yticklabels={Optimized Prices,Current Prices,
No Promotions},
xbar,
xlabel=Profit,
nodes near coords, nodes near coords align={horizontal},
% begin new bit
visualization depends on=x \as \rawx,
every node near coord/.append style={
font=\tiny,
shift={(axis direction cs:\pgfkeysvalueof{/pgfplots/xmin}-\rawx,0)}}
% end new bit
]