# pgfplot log axis more than one tick label per decade (1,2,5,10)

I'd like my log axis to have a reasonable number of tick labels which correspond to "nice" numbers. For example, 1-2-5-10 are nearly a geometric series, so they are about equally spaced on the log axis.

I tried the style log ticks with fixed point and it does stupid stuff like below:

The first axis has not enough labels, leaving huge amounts of space between them. The second axis has chosen to put major ticks halfway between powers of ten -- e.g. square roots of (.001, .01, .1), and doesn't have that telltale logarithmic tick spacing, so readers are unlikely to even notice it is a log scale.

These both look bad. I also tried

    extra x ticks={0.02,0.05,0.1,0.2,0.5,1,2,5,10,20,50,100},
extra x tick label={ \pgfmathfloatparsenumber{\tick} \pgfmathfloatexp{\pgfmathresult} \pgfmathprintnumber{\pgfmathresult} },
xticklabels={},


which comes close

but there are still a couple problems. The labels aren't centered correctly on the ticks, and the right-hand graph is still putting ticks at sqrt(0.001, 0.01, 0.1). And the extra ticks look like major ticks, which isn't really a problem but isn't ideal either.

I do realize that this data range doesn't really need a log scale, it's in the middle region that could go either way. I'm using log for consistency with other data that spans over three decades.

How can I get the proper minor ticks and labels?

Complete MWE

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepgfplotslibrary{statistics}

\pgfplotsset{compat=1.9}

\begin{document}

\pgfplotsset{
S/.style={
xmode=log,
log ticks with fixed point,
ytick={1,2}
},
}

\resizebox{.9 \columnwidth}{!}{
\begin{tikzpicture}
\begin{groupplot}[S,
group style={columns=2},
boxplot/draw direction=x,
]
\nextgroupplot[title={90th Percentile Error}]
0.101411 \\ 0.072893 \\ 0.093314 \\ 0.075811 \\ 0.080916 \\ 0.063481 \\ 0.092617 \\ 0.113238 \\ 0.093469 \\ 0.073329 \\ 0.081212 \\ 0.092889 \\ 0.088329 \\ 0.096120 \\
};
1.032139 \\ 0.791053 \\ 0.659514 \\ 0.700048 \\ 0.523935 \\ 0.779241 \\ 0.891047 \\ 0.626462 \\ 1.141396 \\ 0.959789 \\ 1.110311 \\ 1.205517 \\ 1.058122 \\ 1.278061 \\ 0.801587 \\
};

\nextgroupplot[title={Mean Absolute Error}]
0.051834 \\ 0.037930 \\ 0.057205 \\ 0.037753 \\ 0.033726 \\ 0.030318 \\ 0.046359 \\ 0.056751 \\ 0.036272 \\ 0.038457 \\ 0.039852 \\ 0.040726 \\ 0.047210 \\ 0.047129 \\ 0.042315 \\ 0.052287 \\ 0.048230 \\ 0.044014 \\ 0.039751 \\ 0.059421 \\ 0.043587 \\ 0.035238 \\ 0.039131 \\ 0.034533 \\ 0.045348 \\ 0.037391 \\ 0.048204 \\
};
0.334964 \\ 0.316966 \\ 0.269175 \\ 0.251022 \\ 0.225320 \\ 0.245171 \\ 0.322015 \\ 0.281091 \\ 0.367715 \\ 0.336092 \\ 0.375951 \\ 0.467985 \\ 0.407952 \\ 0.289091 \\
};
\end{groupplot}
\end{tikzpicture}
}

\end{document}


Here's the final result:

The key to get rid of those sqrt values is to use none of the default ticks. Then you lose the default minor ticks, so you have to define minor ticks manually:

logaxisx/.style={
xmode=log,
xtick={0.02,0.05,0.2,0.5,2,5,20,50},
extra x ticks={0.01,0.1,1,10,100},
minor xtick={0.03,0.04,0.06,0.07,0.08,0.09,0.3,0.4,0.6,0.7,0.8,0.9,3,4,6,7,8,9,30,40,60,70,80,90},
%xticklabel={\pgfmathfloatparsenumber{\tick}\pgfmathfloatexp{\pgfmathresult}\pgfmathprintnumber{\pgfmathresult}},
log ticks with fixed point,
extra x tick style={/pgfplots/major tick length=.2cm,/pgfplots/tick style={line width=1.5pt}},
}


The bad alignment was caused by extraneous spaces in the xticklabel value when I copied it from the gallery, which have also been fixed.

The last extra x tick style differentiates decades from the other labelled ticks, if desired.

Quite a bit of extra code for something that, IMO, should be the default result of log ticks with fixed point.

UPDATE: I tried log ticks with fixed point again, and it does set the right numeric formatting, so the complicated xticklabel isn't necessary. Thanks Christian for that. But it is still necessary to manually set the xtick, extra x ticks, and minor xtick lists.

This here is actually more a comment than an answer. But since it is too long and contains an image, I would like to post it here.

The short comment is: I cannot reproduce the problem. I tried it with

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.9}

\begin{document}

\pgfplotsset{
S/.style={
scale only axis,
height=1cm,
width=8cm,
axis x line=bottom,
axis y line=none,
ytick=\empty,
xmin=0.1,xmax=10,
enlarge x limits,
ymin=-1,ymax=1,
xtick={0.1,0.2,0.5,1,2,5,10},
},
}

\begin{tikzpicture}
\begin{semilogxaxis}[
S,
log ticks with fixed point,
]
\end{semilogxaxis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{semilogxaxis}[S]
\end{semilogxaxis}
\end{tikzpicture}
\end{document}


From what I see, the descriptions are correct up to the fact that it is just "almost" a geometric series.

I left extra x ticks out of the game for now since it appears to be part of the solution rather than part of the question.

May I conclude my "answer comment" with a request to provide a full minimal working example such that we can see the whole picture of what you are trying to achieve and how the problem can be reproduced? Such a minimal working example is good practice anyway.

• Sorry, I forgot to mention that I don't want to lose the minor ticks, which are an immediate visual indication of being a log axis. I'll work up an MWE. Oct 25 '14 at 13:04
• MWE added to question. Because groupplot uses no \begin{axis} I'm using xmode=log instead of semilogxaxis. Oct 25 '14 at 13:37
• After trying some different things, I'm now convinced that 1-2-5-10 is the only set of intra-decade labels that a log ticks with fixed point scale will ever need. If you have some many decades that the labels run together, simply switch to 1-10-100 (one per decade). And if the plot has so few decades that more ticks are needed, it probably makes sense as linear, not logarithmic, anyway. Some of my own plots span three decades, the ones with less than 2 decades could use linear scale; I'm using log only for consistency. Oct 25 '14 at 17:01
• Well, that might be true for your application. In general, 1-2-5-10 is unsuitable since it has an uneven space and applies only to a very limited number range (even if we include 1-10-100). Note furthermore that log ticks with fixed point currently affects the number format, not the choice of tick positions. It might make sense to allow more presets of tick positions, but that is a different story. Oct 25 '14 at 17:47
• Hm, yes I start to really see the problem. I agree that the result of the combination: ranges around 1, default tick spacing, and log ticks with fixed point is useless. But I still see a need to define what is the correct solution. Perhaps we should discuss this outside of this Q&A forum; maybe mail is a good idea. My address is on top of the pgfplots manual. Oct 25 '14 at 18:31