34

Assume you have a trigonometric function, say for simplicity sin(x). When plotting the function, how can one place the x-axis markers on 0, pi/2, pi, 3pi/2, 2pi, instead of the default numeric values that pgfplots uses?

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
D R
  • 455

3 Answers3

41

Use \xtick to specify where you want the ticks, and \xticklabels to specify the corresponding labels.

\documentclass{article} 
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis x line=center, 
    axis y line=middle, 
    xtick={0,1.5708,3.14159,4.7123889},
    xticklabels={$0$,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$},
    domain=0:2*pi
]
\addplot {sin(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}

If you don't want a label a particular tick mark, just leave that one blank. For example, if you did not want the label at pi you would replace the line with xticklabels={$0$,$\frac{\pi}{2}$,,$\frac{3\pi}{2}$}.

Peter Grill
  • 223,288
  • This code puts the labels at the wrong places. – D R Jul 22 '11 at 00:20
  • @celil: Can you clarify what you mean. The location looks correct to me. I see that you asked for one at 2pi, so do you mean that the above example is missing that? If so, you can just add one more label. – Peter Grill Jul 22 '11 at 00:57
  • I take my comment back. For some reason when I compiled before, the x-axis ticks did not correspond to the correct positions, but now the ticks are at the correct positions. – D R Jul 24 '11 at 21:06
  • 1
    Am i wrong or the thick mark at 0 does not show up? – mmj Jun 18 '13 at 17:30
  • @mmj: Hmmm... Appears as if you are correct. Perhaps you should post a follow up question. – Peter Grill Jun 19 '13 at 00:04
  • 1
    It seems the problems has already be addressed: http://tex.stackexchange.com/q/78763/28685 . In your MWE axis y line=left should replace axis y line=middle. – mmj Jun 19 '13 at 13:34
  • @mmj: Thanks for finding that. If this question gets edited, I'll add that link to the answer. – Peter Grill Jun 19 '13 at 13:36
  • I have a question regarding the use of pi as a number in this example. In the \xtick command you used 3.14, but in the \domain command you seem to use pi as a numeric value. How can I use pi generally as numeric value? – Christine Aug 16 '16 at 09:14
  • @Christine: Looks like xtick require a numerical value, but the parser for the domain seems to allow one to use pi. Might be good to post a good follow-up qustion. – Peter Grill Aug 16 '16 at 21:11
7

You can specify the position of the ticks using the xtick and scaled x ticks options. Example:

\documentclass{article} 
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xtick={0,1.5708,...,10},domain=0:2*pi,scaled x ticks={real:3.1415},
  xtick scale label code/.code={$\cdot \pi$}]
\addplot {sin(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}

Taken from page 183 of the manual.

Spike
  • 6,729
6

Using gnuplot, I suggest the following:

\documentclass{minimal}
\usepackage{tikz}
% GNUPLOT required
\begin{document}
\begin{center}
  \begin{tikzpicture}[domain=0:6.3]
    \foreach \x/\xtext in {0/0, 0.5*pi/\frac{\pi}{2} ,pi/\pi,
      1.5*pi/\frac{3\pi}{2}, 2*pi/2\pi}
    \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$\xtext$};
    \draw[very thin,color=gray] (-0.1,-1.1) grid (2*pi,1.1); 
    \draw[color=blue] plot[id=sin] function{sin(x)} node[right] {$f(x) = \sin x$};
  \end{tikzpicture}
\end{center}
\end{document}

Which is maybe simpler. Maybe not... I'm using explicit representation of the axis' labels. The result

Dror
  • 22,613