Matplotlib.pyplot.locator_params() in Python
Last Updated :
21 Apr, 2020
Matplotlib is one of the most popular Python packages used for data visualization. It is a cross-platform library for making 2D plots from data in arrays.Pyplot is a collection of command style functions that make matplotlib work like MATLAB.
Note: For more information, refer to Python Matplotlib – An Overview
locator_params() is used for controlling the behaviors of tick locators. The attribute axis is for specifying on which axis is the function being applied.
# for Y axis
matplotlib.pyplot.locator_params(axis='y', nbins=3)
# for X axis
matplotlib.pyplot.locator_params(axis='x', nbins=3)
# for both, x-axis and y-axis: Default
matplotlib.pyplot.locator_params(nbins=3)
Reducing the maximum number of ticks and use tight bounds:
plt.locator_params(tight=True, nbins=4)
Example 1:
import matplotlib.pyplot as plt
y = [ - 1 , 4 , 9 , 16 , 25 ]
x = [ 1 , 2 , 3 , 4 , 5 ]
plt.locator_params(axis = 'x' , nbins = 5 )
axes = plt.axes()
axes.grid()
plt.plot(x, y, 'mx' , color = 'green' )
plt.ylim(ymin = - 1.2 , ymax = 30 )
plt.margins( 0.2 )
plt.show()
|
Output:
Example 2:
import matplotlib.pyplot as plt
def for_lines(xlab, ylab, plot_title,
size_x, size_y, content = []):
width = len (content[ 0 ][ 1 :])
s = [x for x in range ( 1 , width + 1 )]
plt.figure(figsize = (size_x, size_y))
for line in content:
plt.plot(s, line[ 1 :], 'ro--' ,
color = 'green' ,
label = line[ 0 ])
plt.title(plot_title)
plt.xlabel(xlab)
plt.ylabel(ylab)
t = len (s)
plt.locator_params(nbins = t)
for_lines( "x-axis" , "y-axis" ,
"GeeksForGeeks" , 7 , 7 ,
[[ 1 , 2 , 4 , 3 , 5 ]])
|
Output:
Example 3:
import matplotlib.pyplot as plt
plt.locator_params(nbins = 10 )
plt.plot([ 1 , 2 , 3 , 5 , 7 ],
[ 2 , 3 , 9 , 15 , 16 ],
'ro-' , color = 'red' )
plt.show()
|
Output:
Share your thoughts in the comments
Please Login to comment...