8

I am trying to make a plot with a base-2 logarithmic axis and everything is turning out great, except that the labels are 32, 64, 128, 256, 512, and 1020. The last one should be 1024. I assume that this has something to do with significant digits, but I'm not sure how to change it.

Here is the code that I am using:

\documentclass{article}
\usepackage{pgfplots}
\usepackage[active, tightpage]{preview}
\PreviewEnvironment{tikzpicture}

\begin{document}
\begin{tikzpicture}
    \begin{semilogxaxis}[
        every axis legend/.append style={nodes={right}},
        xlabel=$n$,
        log basis x=2,
        ymode=log,
        ylabel=Time (sec),
        log ticks with fixed point,
        legend style={at={(0.05,0.95)},
        title={My title},
        anchor=north west}]
        \addplot coordinates {
            (32,0.04)
            (64,0.09)
            (128,0.14)
            (256,0.27)
            (512,0.56)
            (1024,1.66)
        };
        \addplot coordinates {
            (32,0.04)
            (64,0.09)
            (128,0.26)
            (256,0.92)
            (512,5.71)
            (1024,42.14)
        };
        \legend{With minimization,Without minimization}
    \end{semilogxaxis}
\end{tikzpicture}
\end{document}

Thanks so much.

Habi
  • 7,694
Gregory
  • 203
  • 1
    It would be great if you can not just provide the code, but a full minimal working example. I've edited your question to add one, this might help with getting an answer from procrastinating TikZ/PGF experts... – Habi Jan 12 '16 at 09:15
  • It definitely seems to be a rounding/significant digit problem. When I add xtick={32,64,128,256,512,999} on line 15, eveything is fine. Adding xtick={32,64,128,256,512,1001} adds a rounded down 1000 to the x-axis, which is obviously also the case for 1024 (or 1020). I don't have an answer though... – Habi Jan 12 '16 at 09:24

1 Answers1

6

This is indeed a precision issue.

Two possible options:

  • Use

    xticklabel={
        \pgfkeys{/pgf/fpu=true}
        \pgfmathparse{int(2^\tick)}
        \pgfmathprintnumber[fixed]{\pgfmathresult}
    }
    

    This gives the correct results up to 2^21 = 2097152, after that, you also run into precision problems

  • Load the xint package (by our own jfbu!) and then use

    xticklabel={\xinttheiexpr2^\tick\relax}
    

    This works up to a few hundred digits. I tried it up to 2^42 = 4398046511104, which works correctly. At some point, you probably don't want fixed format output anymore, though.


\documentclass{article}
\usepackage{pgfplots}
\usepackage{xintexpr}
\usepackage[active, tightpage]{preview}
\PreviewEnvironment{tikzpicture}

\begin{document}
\begin{tikzpicture}
    \begin{semilogxaxis}[
        every axis legend/.append style={nodes={right}},
        xlabel=$n$,
        log basis x=2,
        ymode=log,
        ylabel=Time (sec),
        /pgf/number format/1000 sep={\,},
        xticklabel={\xinttheiexpr[0]2^\tick\relax},
        legend style={at={(0.05,0.95)},
        title={My title},
        anchor=north west}]
        \addplot coordinates {
            (32,0.04)
            (64,0.09)
            (128,0.14)
            (256,0.27)
            (512,0.56)
            (1024,1.66)
        };
        \addplot coordinates {
            (32,0.04)
            (64,0.09)
            (128,0.26)
            (256,0.92)
            (512,5.71)
            (1024,42.14)
        };
        \legend{With minimization,Without minimization}
    \end{semilogxaxis}
\end{tikzpicture}
\end{document}
Jake
  • 232,450
  • When I tried your first suggestion, I got the following error: 'Argument of \pgfflt@readlowlevelfloat has an extra }.' It seems that the problem is with the final line, i.e. '\pgfmathprintnumber[fixed]{\pgfmathresult}'. Do I need something for that? – Gregory Jan 14 '16 at 01:14
  • Perhaps I am using an old version of pgfplots? I am on version 1.9. Is this only available in a newer version? – Gregory Jan 14 '16 at 01:30
  • I just now encountered the same issue as Gregory; notably, this had not happened previously, when the document I was working with was smaller, and when I then tried the xint version it failed as well. My fix that seems to work for at least small values (<10000, didn't test with more) is to switch to /pgf/fpu=false (apparently the default, but making it explicit here is probably good anyway). – JAB Apr 04 '16 at 15:01