Open In App

How to Change the Number of Ticks in Matplotlib?

Last Updated : 28 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Changing the number of ticks in Matplotlib improves the clarity of a plot by controlling how many tick marks appear along the axes. This can make the chart easier to read and interpret, especially when dealing with dense or sparse data. You can increase or decrease the number of ticks depending on the level of detail you want to show.

Using MaxBLocator

MaxNLocator from matplotlib.ticker automatically places "nice" tick locations and limits their number. It's highly efficient and ideal for dynamic plots where the range isn't fixed.

Python
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

x = [1, 2, 3, 4]
y = [7, 13, 24, 22]

plt.plot(x, y)
plt.title("Plot")

plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")

# Limit max ticks on x and y axis
plt.gca().xaxis.set_major_locator(MaxNLocator(nbins=4))
plt.gca().yaxis.set_major_locator(MaxNLocator(nbins=4))

plt.show()

Output

Output
Using MaxBLocator

Explanation: MaxNLocator(nbins=4) limits the number of major ticks on the axes to a maximum of 4. plt.gca() gets the current axes, and set_major_locator() applies the locator to control tick placement.

Using locator_params()

locator_params() is a quick way to set the number of ticks across axes with minimal configuration.

Python
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

x = [1, 2, 3, 4]
y = [7, 13, 24, 22]

plt.plot(x, y)
plt.title("Plot")

plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")

plt.locator_params(axis='both', nbins=4)
plt.show()

Output

Output
Using locator_params

Explanation: plt.locator_params(axis='both', nbins=4) sets the maximum number of major ticks to 4 for both x and y axes.

Using xticks() and yticks()

xticks() and yticks() provide full manual control over tick positions and labels. Use this method when you know the exact locations.

Python
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

x = [1, 2, 3, 4]
y = [7, 13, 24, 22]

plt.plot(x, y)
plt.title("Plot")

plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")

plt.xticks([1, 2, 3, 4])
plt.yticks([7, 13, 24, 22])
plt.show()

Output

Output
Using xticks() and yticks()

Explanation: plt.xticks([1, 2, 3, 4]) and plt.yticks([7, 13, 24, 22]) explicitly set the positions of the ticks on the x and y axes.

Using xlim() / ylim()

This method restricts the axis range using xlim() or ylim(), then applies locator_params() to control tick density. It's useful when you want to focus on a subset of the data.

Python
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

x = [1, 2, 3, 4]
y = [7, 13, 24, 22]

plt.plot(x, y)
plt.title("Plot")

plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")

plt.xlim(0, 3)  # restrict x-axis range
plt.locator_params(axis='x', nbins=3)
plt.show()

Output

Output
Using xlim() and ylim()

Explanation: plt.xlim(0, 3) restricts the visible x-axis range from 0 to 3, trimming any data outside this interval. plt.locator_params(axis='x', nbins=3) then suggests a maximum of 3 ticks within this limited range.


Next Article
Article Tags :
Practice Tags :

Similar Reads