25

I'm trying to plot some data where the X axis is logarithmic. The data runs from ~30 microseconds up to 10 milliseconds. It looks much cleaner to have the x-ticks looking like

{0.1 ms, 1 ms, 10 ms}

than

{10^-4 s, 10^-3 s, 10^-2 s}.

In other words, I would like my tick labels to be presented in fixed point (i.e., not as exponentials), and scaled (multiplied by 1000).

To achieve this effect, I've tried using

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{semilogxaxis}
    [xmin=1e-6, xmax=1e-3, domain=1e-6:1e-3,
    scaled x ticks=real:1e-3,
    xtick scale label code/.code={},
    log ticks with fixed point]
    \addplot {x};
  \end{semilogxaxis}
\end{tikzpicture}
\end{document}

but logarithmic axes seem to ignore the "scaled x ticks" instructions. Any help would be much appreciated.

Thanks,

2 Answers2

38

It appears as if you want to rescale the x coordinates without extracting some common factor. The scaled x ticks feature has the main use case of generating a common tick factor which is placed into some node... and, in fact, pgfplots has no builtin support for scaled ticks and log axes as it is typically no use-case.

However, rescaling the x coordinates is a use-case, and it is quite simple to implement by means of x filter:

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmode=log,
    log ticks with fixed point,
    % for log axes, x filter operates on LOGS.
    % and log(x * 1000) = log(x) + log(1000):
    x filter/.code=\pgfmathparse{#1 + 6.90775527898214},
]
\addplot table {
0.0001 10
0.001 20
0.01 15
};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • 2
    The units library currently doesn't work properly with a logarithmic axis. In a linear axis, adding x unit=m, change x base=true, x SI prefix=milli, would scale all the ticks by a factor of 1000, but that doesn't work if you add xmode=log. – Jake Apr 14 '13 at 08:02
  • Thanks for the answer Christian, and also thanks for providing PGFPlots and maintaining it! Producing pretty plots is now one thing less that I need to worry about in my PhD! – sircolinton Apr 15 '13 at 11:19
  • 1
    Although, when I try to compile the above example with LaTeX, I get the error:
    "! Illegal unit of measure (pt inserted)."
    
    

    Using pgfplots 1.8 and TeX Live 2012 on Gentoo Linux.

    – sircolinton Apr 15 '13 at 11:59
3

The problem isn't with the logarithmic axis per se, but with the log ticks with fixed point style, which ignores the scaled x ticks option.

Here's a slightly modified version that checks whether the scaling is active and applies it to the fixed point tick labels. There is a drawback, though: This won't work if you're using a logarithmic y axis with a different scaling. I think it would be good if you could open a bug report for this.

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{units}

\makeatletter
\pgfplotsset{
    /pgfplots/log ticks with fixed point/.style={
        /pgfplots/log number format basis/.code 2 args={
            \begingroup
            \edef\pgfplots@exponent{##2}%
            \pgfkeysalso{/pgf/fpu}%
            % configure the style to avoid crap like
            % 10,000.2  or 0.000999937 :
            \pgfqkeys{/pgf/number format}{%
                fixed relative,
                precision=3,
            }%
            \ifdim##1pt=10pt
                \def\pgfplots@baselog{2.30258509299405}%
            \else
                \pgfmathparse{ln(##1)}%
                \let\pgfplots@baselog=\pgfmathresult
            \fi
            \ifdefined\pgfplots@scaled@ticks@x@arg\pgfmathfloatparsenumber{\pgfplots@scaled@ticks@x@arg}\else\def\pgfmathresult{1}\fi%
            \pgfmathparse{\pgfmathresult*exp(\pgfplots@exponent*\pgfplots@baselog)}%
            \pgfmathprintnumber[#1]\pgfmathresult
            \endgroup
        },
    }
}
\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmode=log,
    log ticks with fixed point,
    scaled x ticks=real:1e3
]
\addplot table {
0.0001 10
0.001 20
0.01 15
};
\end{axis}
\end{tikzpicture}
\end{document}

Jake
  • 232,450
  • Thanks very much for your code example, Jake. I submitted the bug report. Since I don't know anything about TeX or PGFPlots, I couldn't add anything more than you mentioned:

    http://sourceforge.net/tracker/?func=detail&aid=3610456&group_id=224188&atid=1060656

    – sircolinton Apr 10 '13 at 11:19