Matplotlib.axis.axis.set_major_locator()
matplotlib库的Axis模块中的Axis.set_major_locator()函数用于设置主时钟的定位符。
语法:Axis.set_major_locator(self, locator)
参数:该方法接受以下参数。
- 定位器:该参数是 定位器 .
返回值:该方法不返回任何值。
下面的例子演示了matplotlib.axis.axis.set_major_locator()函数在matplotlib.axis中的作用:
示例1
# Implementation of matplotlib function
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()
输出:
示例2
# 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 = 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()
输出: