Matplotlib.axis.Axis.set_major_locator() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack.
Matplotlib.axis.Axis.set_major_locator() Function
The Axis.set_major_locator() function in axis module of matplotlib library is used to set the locator of the major ticker.
Syntax: Axis.set_major_locator(self, locator)
Parameters: This method accepts the following parameters.
- locator: This parameter is the Locator.
Return value: This method does not returns any value.
Below examples illustrate the matplotlib.axis.Axis.set_major_locator() function in matplotlib.axis:
Example 1:
Python3
from matplotlib.axis import Axis
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.cbook as cbook
years = mdates.YearLocator()
months = mdates.MonthLocator()
years_fmt = mdates.DateFormatter( '% Y' )
with cbook.get_sample_data( 'goog.npz' ) as datafile:
data = np.load(datafile)[ 'price_data' ].view(np.recarray)
fig, ax = plt.subplots()
ax.plot( 'date' , 'adj_close' , data = data, color = "green" )
Axis.set_major_locator(ax.xaxis, years)
ax.set_ylim(( 100 , 300 ))
ax.format_xdata = mdates.DateFormatter( '% m' )
ax.grid( True )
fig.suptitle("Matplotlib.axis.Axis.set_major_locator()\n\
Function Example", fontsize = 12 , fontweight = 'bold' )
plt.show()
|
Output:
Example 2:
Python3
from matplotlib.axis import Axis
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
x = [ 0 , 5 , 9 , 10 , 15 ]
y = [ 0 , 1 , 2 , 3 , 4 ]
tick_spacing = 1
fig, ax = plt.subplots( 1 , 1 )
ax.plot(x, y)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
fig.suptitle("Matplotlib.axis.Axis.set_major_locator()\n\
Function Example", fontsize = 12 , fontweight = 'bold' )
plt.show()
|
Output:
Last Updated :
10 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...