3

In the following code, I want the labels of the x-axis to be 128,256,1024,2048,4096. However, when I compile it I get 128,256,1020,2050,4100. What am I missing?

\documentclass{article}
\usepackage{pgfplots}
\usepackage{amsmath}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xlabel={x},
    ylabel={y},
    width=260pt,
    x tick label style={major tick length=0pt},
    xmode=log,
    log ticks with fixed point,
    xtick=data,
    ytick={9,10,11,12,13,14,15,16,17,18,19,20},
    minor y tick num=1,
    ymajorgrids,
    legend style={at={(0.5,0.97)},
    legend entries={{$\epsilon=\tfrac{1}{17}$,},{$\epsilon=\tfrac{1}{16}$,},{$\epsilon=\tfrac{1}{15}$,},$\epsilon=\tfrac{1}{14}$},
    anchor=north,legend columns=-1},
    ybar,
    bar width=5pt,
  ]
  \addplot[fill=red!80,
     error bars/.cd, 
     y dir=plus, 
     y explicit,
    ]
   plot coordinates { 
       (128,9.217) += (0,4.783)
       (256,10.128) += (0,4.872)
       (512,11.125) += (0,5.875)
       (1024,11.918) += (0,5.082)
       (2048,12.785) += (0,5.215)
       (4096,13.994) += (0,5.006)
   };
 \end{axis}
 \end{tikzpicture}
 \end{document}
6
  • 1
    welcome to tex.se! please extend your code snippet to complete, but small, document (beginning with \documentclas{...} and ending with `\end{document}. help us to help you. – Zarko Feb 12 '18 at 23:05
  • 1
    To make @Zarko happy, please add begin{tikzpicture} before\begin{axis}. – user121799 Feb 12 '18 at 23:41
  • My bad, it's fixed now. – pgfuseranonymous Feb 13 '18 at 0:10
  • Perhaps this is related. At least I interpret it as: if you want xmode=log, you may need to set your labels manually. – user121799 Feb 13 '18 at 0:34
  • @marmot Adding xtick={128,256,512,1024,2048,4096} doesn't help. – pgfuseranonymous Feb 13 '18 at 0:43
1

Quick and dirty, I know, but for the moment you might use xticklabels={128,256,512,1024,2048,4096} (or xticklabels={128,256,512,{1,024},{2,048},{4,096}}, if you prefer the thousands separator).

2

Without checking the definition of log ticks with fixed point I guess the shown result is because of some rounding of the numbers. So just define your own style and choose the rounding you need.

For details please have a look at the comments in the code.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.14,
        % define your own x tick label style
        % (adapted from <https://tex.stackexchange.com/a/139084/95441>)
        log x ticks with fixed point/.style={
            xticklabel={
                \pgfkeys{/pgf/fpu=true}
                \pgfmathparse{exp(\tick)}%
                \pgfmathprintnumber[fixed,precision=0]{\pgfmathresult}
                \pgfkeys{/pgf/fpu=false}
            },
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xlabel={$x$},
        ylabel={$y$},
        width=260pt,
        xmode=log,
        % replace the "default" style ...
%        log ticks with fixed point,
        % ... with the custom style
        log x ticks with fixed point,
        xtick=data,
        x tick label style={
            major tick length=0pt,
        },
        ytick={9,...,20},
        minor y tick num=1,
        ymajorgrids,
        legend style={
            at={(0.5,0.97)},
            anchor=north,
            legend columns=-1,
        },
        legend entries={
            {$\epsilon=\tfrac{1}{17}$,},
            {$\epsilon=\tfrac{1}{16}$,},
            {$\epsilon=\tfrac{1}{15}$,},
            $\epsilon=\tfrac{1}{14}$%
        },
        ybar,
        bar width=5pt,
    ]
        \addplot[
            fill=red!80,
            error bars/.cd,
                y dir=plus,
                y explicit,
        ]  coordinates {
           (128,9.217)   += (0,4.783)
           (256,10.128)  += (0,4.872)
           (512,11.125)  += (0,5.875)
           (1024,11.918) += (0,5.082)
           (2048,12.785) += (0,5.215)
           (4096,13.994) += (0,5.006)
        };
     \end{axis}
 \end{tikzpicture}
 \end{document}

image showing the result of above code

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.