1

I have a problem with the xticks in my PGFplot that I'm not sure how to even start troubleshooting. The tick labels are repeated and misaligned; basically they are all over the place.

enter image description here

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{amsmath}
\pgfplotsset{compat=newest}

\usepackage{filecontents}

\begin{filecontents*}{iso.tsv}
range   X   Y   Z
0--9    9841063 9847764 9840032
10--99  38622   32503   39579
100--999    3116    2672    3197
1000--9999  6054    6019    6057
10000--99999    563 460 553
100000+ 0.1 0.1 0.1
\end{filecontents*}

\begin{document}

    \def\mystrut{\vphantom{$[1^1$}}     
    \pgfplotstableread{iso.tsv}\mydata


    \begin{tikzpicture}
    \begin{axis}[
    ymode = log,
    ybar,
    ytick={10,100,1000,10000,100000,1000000,10000000},
    xtick style={draw=none},
    bar width=.35cm,
    width=15cm,
    height=6cm,
    ymajorgrids=true,
    yminorgrids=false,
    legend style={font=\footnotesize},
    legend style={at={(0.99,0.99)},
        anchor=north east,legend columns=1},
    symbolic x coords={0--9,10--99,100--999,1000--9999,10000--99999,100000+},
    x tick label style={font=\mystrut},
    %xticklabels={a,b,c,d,e,f},
    ymin=1,ymax=15000000,
    ylabel={Number of graphs},
    xlabel={Time taken (ms)},
    %enlarge x limits=1
    ]

    \addplot table[x=range,y=X]{\mydata};
    \addplot table[x=range,y=Y]{\mydata};
    \addplot table[x=range,y=Z]{\mydata};
    \legend{X, Y, Z}
    \end{axis}
    \end{tikzpicture}
\end{document}

I'm sure it must be something quite simple since the plot itself it quite simple, but I cannot seem to find it. Any help much appreciated (once again)!

  • 1
    Add xtick=data to the axis options. – esdd Jun 3 '16 at 21:24
  • I removed xtick=datait because I thought I was giving the xtick (labels) so they weren't coming from the data, but then ... ugh. In any case, thanks! If you wish to post it as an answer, I will accept. – badroit Jun 3 '16 at 21:57
1

symbolic x coords={0--9,10--99,100--999,1000--9999,10000--99999,100000+}, defines only transformations so that the elements of this list are valid input coordinates.

To get the desired result you can use xtick=data. To ensure that also the last tick with the very small values will be used add xmax=100000+. And of course you have to use enlarge x limits.

symbolic x coords={0--9,10--99,100--999,1000--9999,10000--99999,100000+},
xtick=data,
xmin=0--9,xmax=100000+,
enlarge x limits={abs={2*(\pgfplotbarwidth+2pt)}},

enter image description here

Code:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{amsmath}
\pgfplotsset{compat=newest}

\usepackage{filecontents}
\begin{filecontents*}{iso.tsv}
range   X   Y   Z
0--9    9841063 9847764 9840032
10--99  38622   32503   39579
100--999    3116    2672    3197
1000--9999  6054    6019    6057
10000--99999    563 460 553
100000+ 0.1 0.1 0.1
\end{filecontents*}

\begin{document}
    \def\mystrut{\vphantom{$[1^1$}}     
    \pgfplotstableread{iso.tsv}\mydata

    \begin{tikzpicture}
    \begin{axis}[
    ymode = log,
    ybar,
    ytick={10,100,1000,10000,100000,1000000,10000000},
    xtick style={draw=none},
    bar width=.35cm,
    width=15cm,
    height=6cm,
    ymajorgrids=true,
    yminorgrids=false,
    legend style={font=\footnotesize},
    legend style={at={(0.99,0.99)},
        anchor=north east,legend columns=1},
    symbolic x coords={0--9,10--99,100--999,1000--9999,10000--99999,100000+},
    xtick=data,
    xmin=0--9,xmax=100000+,
    enlarge x limits={abs={2*(\pgfplotbarwidth+2pt)}},
    x tick label style={font=\mystrut},
    ymin=1,ymax=15000000,
    ylabel={Number of graphs},
    xlabel={Time taken (ms)},
    ]

    \addplot table[x=range,y=X]{\mydata};
    \addplot table[x=range,y=Y]{\mydata};
    \addplot table[x=range,y=Z]{\mydata};
    \legend{X, Y, Z}
    \end{axis}
    \end{tikzpicture}
\end{document}

It is also possible to use

symbolic x coords={0--9,10--99,100--999,1000--9999,10000--99999,100000+},
xtick={0--9,10--99,100--999,1000--9999,10000--99999,100000+},
xmin=0--9,xmax=100000+,
enlarge x limits={abs={2*(\pgfplotbarwidth+2pt)}},

The result is the same as above.

If you add

xticklabels={a,b,c,d,e,f},

you will get

enter image description here

| improve this answer | |

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.