# How to regulate the decimals in numbers placed above barplots?

I am using the latex package "pgfplots" for producing x-barplots. In these plots the height of each bar is placed as a number to the right of the bar, as shown here:

The code for producing these barplot is given below. Includet in this code is a datafile with x and y numbers, called "data1.dat". My concern is how to regulate the number of decimals shown in the figure. For instance, in the datafile the y-value 1 has the x-value 15.1234, but this number is shown as "15.12" in the figure. More specifically:

(1) How do I set the number of decimals to a specific value, for instance one?

(2) How can I prevent 0.049 to be written 4.9*10-2, but instead as "0.0" if I want one decimal?

(3) How do I make 12.0 to be written as "12.0" instead of "12" as in the figure?

(4) is it possible to exchange 0.049 in the datafile with "<0.05" so that "< 0.05" is shown in the figure?

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{
width=6cm,
height=4cm,
xtick align =outside,
xtick pos = both,
xticklabel pos =lower,
xmin=-5,
xmax = 105,
ytick align = outside,
ytick pos = left,
yticklabel pos=left,
ymin =0,
ymax = 5,
ytick=data,
xbar,
nodes near coords,
every node near coord/.append style = {anchor=west},
}

\begin{document}

% Measurements
\begin{filecontents}{data1.dat}
yvalue         xvalue
1                  15.1234
2                  20.5678
3                  12.0
4                 0.049
\end{filecontents}

\begin{tikzpicture}
\begin{axis}[
yticklabels from table={data1.dat}{yvalue},
xlabel = {x},
ylabel = {y},
every axis y label/.style = {at={(yticklabel cs:0.5)}, rotate=0,anchor =east},
bar width=4pt,
]
y=yvalue,
x=xvalue,
] {data1.dat};
\end{axis}
\end{tikzpicture}

\end{document}

-

As percusse said in the comment, you can force fixed point notation with a specified precision by setting /pgf/number format/fixed, /pgf/number format/precision=1 in the every node near coord/.append style. To make sure that there is always one decimal, even if it is zero, you also need to add /pgf/number format/fixed zerofill.

The last request is trickier, here you need to test the value and then decide what to do with it. This is most easily done using the optional argument of nodes near coords. By default, this value is nodes near coords=\pgfmathprintnumber{\pgfplotspointmeta}, where \pgfmathprintnumber takes care of applying all the formatting options we specified, and \pgfplotspointmeta contains the meta value, which defaults to the x value in an xbar plot, or the y value in most other plot types.

What we want to do is check whether \pgfplotspointmeta is less than 0.05, and print the string $<0.05$ if it is, and \pgfmathprintnumber{\pgfplotspointmeta} otherwise. The intuitive thing to do would be to use \ifdim\pgfplotspointmeta pt<0.05pt ... \else ... \fi, but that doesn't work because \pgfplotspointmeta contains a floating point number (which looks like 1Y4.9e-2]), causing \ifdim to stumble. The best approach here is to switch on the fpu library (which has a much greater number range than pure LaTeX arithmetic), do the comparison using \pgfmathparse, convert the result of the comparison (which is either 1 or 0) to a "normal" fixed point number, switch off the fpu library (otherwise things will break later on), and use \ifdim on the converted result of the comparison.

So if you use

nodes near coords={%
\pgfkeys{/pgf/fpu=true}% Switch on the fpu library
\pgfmathparse{\pgfplotspointmeta<0.05}% Do the comparison
\pgfmathfloattofixed{\pgfmathresult}% convert the result to fixed point
\pgfkeys{/pgf/fpu=false}% switch off the fpu library
\ifdim\pgfmathresult pt=1pt % If the condition was true...
$<0.05$
\else
\pgfmathprintnumber{\pgfplotspointmeta}
\fi
}


you will get

Here's the complete code

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{
width=6cm,
height=4cm,
xtick align =outside,
xtick pos = both,
xticklabel pos =lower,
xmin=-5,
xmax = 105,
ytick align = outside,
ytick pos = left,
yticklabel pos=left,
ymin =0,
ymax = 5,
ytick=data,
xbar,
nodes near coords={%
\pgfkeys{/pgf/fpu=true}%
\pgfmathparse{\pgfplotspointmeta<0.05}%
\pgfmathfloattofixed{\pgfmathresult}%
\pgfkeys{/pgf/fpu=false}%
\ifdim\pgfmathresult pt=1pt
$<0.05$
\else
\pgfmathprintnumber{\pgfplotspointmeta}
\fi
},
every node near coord/.append style = {
anchor=west,
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=1,
},
}

\begin{document}

% Measurements
\begin{filecontents}{data1.dat}
yvalue         xvalue
1                  15.1234
2                  20.5678
3                  12.0
4                 0.049
\end{filecontents}

\begin{tikzpicture}
\begin{axis}[
yticklabels from table={data1.dat}{yvalue},
xlabel = {x},
ylabel = {y},
every axis y label/.style = {at={(yticklabel cs:0.5)}, rotate=0,anchor =east},
bar width=4pt,
]

Ah I was fiddling with scatter/@pre marker code/.prefix code={. That's why it didn't work! – percusse May 30 '12 at 12:54
Oh yeah, that's a hard one to work with. Internally, nodes near coords uses that I think, shielding you from the nitty gritty of @pre marker code. – Jake May 30 '12 at 12:56