8

The standard pgfplots notation for tick labels is without final zeros when no needed but the standard one for scientific papers is to have all ticks with the same number of digits after the dot a part for the 0 that is often print simply as 0.

I am able to set the same precision for all ticks but I would like to print the 0 simply as 0 and not for example 0.0, how can I do that?

The first image is the pgfplots default, the second one is what I am able to do and the third is what I desire to do.

enter image description here

Minimal code for the second image

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}    [ymin=0, ymax=2,
            xmin=0.2, xmax=2,
            y tick label style={
                /pgf/number format/fixed,
                /pgf/number format/fixed zerofill,
                /pgf/number format/precision=1},
            ]
    \addplot[only marks] {x};
\end{axis}
\end{tikzpicture}
\end{document}
  • Beware that there is difference between 0 and 0.0. – Claudio Fiandrino Oct 8 '13 at 9:56
  • @ClaudioFiandrino what do you mean? – Red Oct 8 '13 at 9:57
  • I think Claudio means that the number of significant figures reported can be used to express the uncertainty: Saying c=0.0 implies that the true value could also be 0.04 or -0.03, while c=0 implies that the true value could also be 0.2 or -0.4. This is not as relevant for axis labels, though, where you're basically free to do whatever looks good. If you're interested in this topic, have a look at John Denker's "Measurement and Uncertainties", where the reasoning and pitfalls of the "sig figs" approach are discussed – Jake Oct 8 '13 at 11:02
  • I've been trying to find an example for the style you describe (same number of significant figures for all numbers except zero), but in all publications I looked at, either there are no trailing zeros (your first case) or there are the same number of decimal digits (your second case). Could you point to a published example that represents your third case? – Jake Oct 8 '13 at 11:16
  • @Jake for what regard the difference between 0 and 0.0 I agree with you: it is not so relevant for axis labels.. in addition I am plotting defined functions and not data with uncertainty so in my case it is not relevant at all. For the published example I don't find anything, except in my PhD supervisor's articles... He says to do this because it is commonly done but now I see that it's not true :-) Anyway, if there is a way of doing it, good, if there is not I will point out the situation to him – Red Oct 8 '13 at 11:34
16

This is a pretty unusual requirement (I haven't been able to find any published examples using this format), and I don't think there's a predefined format for this. It looks like you'll have to do this using a conditional:

yticklabel={\ifdim\tick pt=0pt 0 \else\pgfmathprintnumber{\tick}\fi}

will print 0 if the tick value is 0, and feed the number through the number formatter in all other cases:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}    [ymin=0, ymax=2,
            xmin=0.2, xmax=2,
            y tick label style={
                /pgf/number format/fixed,
                /pgf/number format/fixed zerofill,
                /pgf/number format/precision=1
            },
            yticklabel={\ifdim\tick pt=0pt 0 \else\pgfmathprintnumber{\tick}\fi}
            ]
    \addplot[only marks] {x};
\end{axis}
\end{tikzpicture}
\end{document}
  • Ugh, my supervisor wanted this, too. What is that? – fuzzybear3965 Oct 15 '17 at 0:45

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.