34

Now the numbers on the x-axis are

0, 0.5, 1, 1.5, 2

I want to show integer only:

0, 1, 2

How can I do that? My code:

\begin{tikzpicture}
\begin{axis}[
    xlabel=threads,
    ylabel=speedup & deviation]
\addplot table[x=thread,y=speedup] {datafile.dat};
\addplot table[x=thread,y=dev] {datafile.dat};
\end{axis}
\end{tikzpicture}

The content of datafile.dat:

thread  speedup dev
1   1   0
2   3   1
3   5   0
4   7   1
  • 2
    For example, you can include a compilable minimal code and we can have a look together :) – percusse Mar 9 '12 at 2:37
29

If you want to have the x ticks to appear exactly as entered you can use the option xtick=data.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfplotstableread{
thread  speedup dev
1   1   0
2   3   1
3   5   0
4   7   1
}\datafile

\begin{tikzpicture}
\begin{axis}[
    xlabel=threads,
    ylabel=speedup \& deviation,
    xtick=data]
\addplot table[x=thread,y=speedup] {\datafile};
\addplot table[x=thread,y=dev] {\datafile};
\end{axis}
\end{tikzpicture}
\end{document}

or you can enter manually by xtick={1,2,3,4} option

  • 1
    This wouldn't be ideal with large sets of data. – Magpie Mar 12 '13 at 20:01
  • @Magpie then you use explicit ticks or leave it to pgfplots anyway. – percusse Mar 12 '13 at 22:41
32

If you want to specify the step, i.e. the interval of your ticks, you can also use the xtick distance-option. Refer to section 4.15 in the pgfplots manual, Version 1.13. As @Magpie pointed out, this one is easier to use than xtick if use have a large number of ticks you want to show.

Code

\documentclass{minimal}
\usepackage{pgfplots}

% example data file
\usepackage{filecontents}
\begin{filecontents}{datafile.dat}
    thread speedup dev
    1 1 0
    2 3 1
    3 5 0
    4 7 1
\end{filecontents}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xlabel=threads,
        ylabel=speedup \& deviation,
        xtick distance=1
        ]

        \addplot table[x=thread,y=speedup] {datafile.dat};
        \addplot table[x=thread,y=dev] {datafile.dat};
    \end{axis}
\end{tikzpicture}
\end{document}

Output

enter image description here

  • Welcome to TeX - LaTeX! Can you provide some sample code showing this in action. – Andrew Swann Apr 17 '16 at 10:08
  • 2
    This is a really valuable answer. – ahorn Oct 20 '16 at 20:04
  • This is probably the correct answer for the original quenstion in most use cases, thanks! – Czechnology May 23 '17 at 14:45
  • When I try to run this MWE, I get the following error: Package pgfkeys Error: I do not know the key '/tikz/xtick distance', to which you passed '1', and I am going to ignore it. Perhaps you misspelled it. – NCH32 Jan 23 at 8:31
  • If you have a package manager, check if your pgfplots is on version >1.13. If it's not, update it. – paulkernstock Jan 23 at 9:28
14

Section 4.14 of the pgfplots manual describes the xtick option, which gives a list of locations for the ticks. In this case xtick={1,...,4} should do the trick.

Here's an example:

\documentclass{article}
\usepackage{pgfplots}

% example data file
\usepackage{filecontents}
\begin{filecontents}{datafile.dat}
thread speedup dev
1 1 0
2 3 1
3 5 0
4 7 1
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xlabel=threads,
    ylabel=speedup \& deviation,
    xtick={1,...,4}]

\addplot table[x=thread,y=speedup] {datafile.dat};
\addplot table[x=thread,y=dev] {datafile.dat};
\end{axis}
\end{tikzpicture}
\end{document}

(EDIT: Different section numbers for difference versions of the manual)

  • What about axis that go from -4 to 4? – Magpie Mar 12 '13 at 20:01
13

Use xtick={0, 1, 2} to specify where you want the xticks:

enter image description here

Notes:

  • This also necessitated specifying xmin=0 to get the marker for x=0.
  • You also need to \& for the ylabel.

Code:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents*}{datafile.dat}
thread  speedup dev
1   1   0
2   3   1
3   5   0
4   7   1
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xlabel=threads,
    ylabel={speedup \& deviation},
    xtick={0, 1, 2},
    xmin=0
]
\addplot table[x=thread,y=speedup] {datafile.dat};
\addplot table[x=thread,y=dev] {datafile.dat};
\end{axis}
\end{tikzpicture}
\end{document}

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.