[Tex/LaTex] Pgfplots with custom axis markers

pgfplots

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?

Best Answer

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}$}.