Changing the tick frequency on the x or y axis
In matplotlib, you can change the tick frequency on the x or y axis of a plot by using the set_xticks()
or set_yticks()
method of the Axes
class. The method takes a list of positions where you want the ticks to be located, and an optional keyword argument minor
to set minor ticks.
For example, to set the tick frequency on the x-axis of a plot to every 2 units:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xticks(np.arange(0, 11, 2))
plt.show()
To set the tick frequency on the y-axis of a plot to every 0.5 units:
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_yticks(np.arange(-1, 1.1, 0.5))
plt.show()
You can also use set_xticklabels()
or set_yticklabels()
to set custom labels for the ticks.
fig, ax = plt.subplots()
ax.