How to use locator_param() In Python

Locator_params() function that lets us change the tightness and number of ticks in the plots. This is made for customizing the subplots in matplotlib, where we need the ticks packed a little tighter and limited. So, we can use this function to control the number of ticks on the plots.

syntax: matplotlib.pyplot.locator_params(axis=’both’, tight=None, nbins=None **kwargs)

Parameter: 

  • axis – The axis we need to change the number of ticks or tighten them.
  • tight – Takes in a bool value weather the ticks should be tightened or not
  • nbins – number of ticks we should have in the axis.

Now, we are going to limit the number of ticks on both axes by 4

Example: 

In this example, we are implementing the same steps to make a plot, and we want only four ticks on both sides. We can call the locate_params() function and mention the axis, which is both x and y-axis, and pass in a parameter called nbins to mention the number of ticks we want on each axis.

Python3




# Setting x and y values for the plot
x = [1, 2, 3, 4]
y = [7, 13, 24, 22]
  
# Initiating the plot
plt.plot(x, y, color='Red')
plt.title("PLOT")
  
# Setting the x and y labels
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
  
# Setting the number of ticks
plt.locator_params(axis='both', nbins=4)
  
# Showing the plot
plt.show()


Output:

How to Change the Number of Ticks in Matplotlib?

In this article, we will see how to change the number of ticks on the plots in matplotlib in Python.

Similar Reads

Method 1: Using xticks() and yticks()

xticks() and yticks() is the function that lets us customize the x ticks and y ticks by giving the values as a list, and we can also give labels for the ticks, matters, and as **kwargs we can apply text effects on the tick labels....

Method 2: Using locator_param()

...

Method 3: Using xlim()

...