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.
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:
Set the tick frequency on the x-axis of a plot to every 2 units in Python's matplotlib
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:
Set the tick frequency on the y-axis of a plot to every 0.5 units in Python's matplotlib
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_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. The following example combines tick frequency adjustment, custom labels, and the minor argument:
Set custom labels for ticks in Python's matplotlib
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))
ax.set_xticklabels(['0', '2', '4', '6', '8', '10'])
ax.set_yticks(np.arange(-1, 1.1, 0.5))
ax.set_yticks(np.arange(-1, 1.1, 0.25), minor=True)
plt.show()