Ticks & Axis Scales
Ticks are the little marks along an axis, and tick labels are the numbers or words next to them. Matplotlib chooses them automatically, but you'll often want to set them yourself: show every year, format numbers as currency, or switch an axis to a logarithmic scale.
Setting tick positions and labels
ax.set_xticks(positions) chooses where the ticks go; an optional second argument gives the labels:
python · try it yourself
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2 * np.pi, 200)
fig, ax = plt.subplots(figsize=(7, 3.5))
ax.plot(x, np.sin(x))
ticks = [0, np.pi / 2, np.pi, 3 * np.pi / 2, 2 * np.pi]
ax.set_xticks(ticks, ["0", r"$\pi/2quot;, r"$\piquot;, r"$3\pi/2quot;, r"$2\piquot;])
ax.set_yticks([-1, 0, 1])
ax.grid(alpha=0.3)
plt.show()
print("x tick labels:", [t.get_text() for t in ax.get_xticklabels()])x tick labels: ['0', '$\\pi/2