Matplotlib.axis.Axis.set_minor_locator() function in Python
Last Updated :
03 Jun, 2020
Improve
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_minor_locator() Function
The Axis.set_minor_locator() function in axis module of matplotlib library is used to set the locator of the minor ticker.
Syntax: Axis.set_minor_locator(self, locator)
Parameters: This method accepts the following parameters.
- formatter: This parameter is the locator.
Return value: This method does not returns any value.
Below examples illustrate the matplotlib.axis.Axis.set_minor_locator() function in matplotlib.axis:
Example 1:
# Implementation of matplotlib function
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 = 0.4
fig, ax = plt.subplots(1, 1)
ax.plot(x, y)
ax.xaxis.set_minor_locator(ticker.MultipleLocator(tick_spacing))
fig.suptitle("Matplotlib.axis.Axis.set_minor_locator()\n\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
Output:

Example 2:
# Implementation of matplotlib function
from matplotlib.axis import Axis
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange
import numpy as np
date1 = datetime.datetime(2020, 4, 2)
date2 = datetime.datetime(2020, 4, 6)
delta = datetime.timedelta(hours = 6)
dates = drange(date1, date2, delta)
y = np.arange(len(dates))
fig, ax = plt.subplots()
ax.plot_date(dates, y ** 2)
ax.set_xlim(dates[0], dates[-1])
ax.xaxis.set_major_locator(DayLocator())
Axis.set_minor_locator(ax.xaxis, HourLocator(range(0, 25, 6)))
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
ax.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S')
fig.autofmt_xdate()
fig.suptitle("Matplotlib.axis.Axis.set_minor_locator()\n\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
Output:
