Using pgfplots
, I often want to label the horizontal axis major ticks in units of pi
or pi/2
, or pi/3
. Is it possible to have such "trigonometric major ticks" use minor tick num = <value>
to specify the number of minor ticks?
Currently my approach for "trigonometric major ticks" is to make a style wholes
that specifies the xtick=<integer multiples of pi>
and xticklabels=<labels>
. I specify the ticks for a range -4*pi to +4*pi, and crop the graph as needed. I similarly have a style called halves
for half-integer multiples of pi:
\documentclass{standalone}
\usepackage{pgfplots} %This package also loads tikz
\pgfplotsset{
% Global Styles
axis lines = middle,
xlabel = $x$,
ylabel = $y$,
no markers,
samples=50,
grid = both,
trig format plots=rad,
enlargelimits = false,
axis line style = {line width=0.5pt},
every axis plot/.append style={
line width = 1.25pt,
smooth,
},
% Label every pi
halves/.style={
xtick = {-12.5664, -10.9956, -9.42478, -7.85398, -6.28319, -4.71239, -3.14159, -1.5708, 0, 1.5708, 3.14159, 4.71239, 6.28319, 7.85398, 9.42478, 10.9956, 12.5664, 14.1372, 15.708, 17.2788, 18.8496, 20.4204, 21.9911, 23.5619, 25.1327},
xticklabels = {$-4\pi$,$-\frac{7\pi}{2}$,$-3\pi$,$-\frac{5\pi}{2}$,$-2\pi$,$-\frac{3\pi}{2}$,$-\pi$,$-\frac{\pi}{2}$,$0$,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$,$\frac{5\pi}{2}$,$3\pi$,$\frac{7\pi}{2}$,$4\pi$,$\frac{9\pi}{2}$,$5\pi$,$\frac{11\pi}{2}$,$6\pi$,$\frac{13\pi}{2}$,$7\pi$,$\frac{15\pi}{2}$,$8\pi$}
},
% Label every pi
wholes/.style={
xtick = {-12.5664, -9.42478, -6.28319, -3.14159, 0., 3.14159, 6.28319, 9.42478, 12.5664, 15.708, 18.8496, 21.9911, 25.1327},
xticklabels = {$-4\pi$,$-3\pi$,$-2\pi$,$-\pi$,$0$,$\pi$,$2\pi$,$3\pi$,$4\pi$,$5\pi$,$6\pi$,$7\pi$,$8\pi$}
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[wholes,domain=-pi:3*pi,ymin=-2,ymax=+4,minor tick num = 1]
\addplot {1-2*cos(2*x)};
\legend{$1-2\cos(2x)$}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[halves,domain=-pi:pi,ymin=-2,ymax=+4,minor tick num = 1]
\addplot[red] {2+0.5*sin(4*x)};
\legend{$2+0.5\sin(4x)$}
\end{axis}
\end{tikzpicture}
\end{document}
with output
Problem: I'm fairly happy with this solution, except minor tick num=1
is not working on the x-axis. I think this is due to rounding errors. Minor grid in pgfplot not showing when specifying ticks manualy
Certainly I could specify the minor ticks manually. I really don't want to do this---I need to create many graphs like this, some times I want minor tick num = 1
, sometimes I want minor tick num = 3
.
Is there a better approach?
minor tick num = <value>
minor tick num