How to Change the Number of Ticks in Matplotlib?
Last Updated :
28 May, 2025
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
Using MaxBLocatorExplanation: 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
Using locator_paramsExplanation: 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
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
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.
Similar Reads
How to Change the Size of Figures in Matplotlib? Matplotlib provides a default figure size of 6.4 inches in width and 4.8 inches in height. While this is suitable for basic graphs, various situations may require resizing figures for better visualization, presentation or publication. This article explores multiple methods to adjust the figure size
3 min read
Change the x or y ticks of a Matplotlib figure Matplotlib is a plotting library in Python to visualize data, inspired by MATLAB, meaning that the terms used (Axis, Figure, Plots) will be similar to those used in MATLAB. Pyplot is a module within the Matplotlib library which is a shell-like interface to Matplotlib module. There are many ways to c
5 min read
Setting the Number of Ticks in plt.colorbar in Matplotlib? Matplotlib is a powerful library for creating visualizations in Python. One of its features is the ability to add colorbars to plots, which can be customized in various ways. A common requirement is to set the number of ticks on a colorbar, especially when the default number of ticks leads to overla
5 min read
How to change the size of axis labels in Matplotlib? Matplotlib is a Python library that helps in visualizing and customizing various plots. One of the customization you can do is to change the size of the axis labels to make reading easier. In this guide, weâll look how to adjust font size of axis labels using Matplotlib.Letâs start with a basic plot
2 min read
How to Rotate X-Axis Tick Label Text in Matplotlib? Rotating X-axis tick labels in Matplotlib improves readability, especially when labels are long or overlap. Angles like 45° or 90° help display dates or categories clearly. Matplotlib provides several easy ways to rotate labels at both the figure and axes levels. Letâs explore the most effective one
3 min read