Take the 2-minute tour ×
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 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
        ]
        \addplot table {
            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
        ]
        \addplot table {
            x y 
            1000 3
            1100 2
            1200 1
        };
    \end{axis}
\end{tikzpicture}
\end{document}

I get the output below:

enter image description here

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?

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

1 Answer 1

up vote 4 down vote accepted

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)}}

enter image description here

(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
        ]
        \addplot table {
            x y 
            1000 3
            1100 2
            1200 1
        };
    \end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer

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.