Python中的 Matplotlib.axis.Axis.set_major_locator()函数
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。它是Python中用于二维数组图的惊人可视化库,用于处理更广泛的 SciPy 堆栈。
Matplotlib.axis.Axis.set_major_locator()函数
matplotlib 库的轴模块中的Axis.set_major_locator()函数用于设置主要股票代码的定位器。
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.
下面的示例说明了 matplotlib.axis 中的 matplotlib.axis.Axis.set_major_locator()函数:
示例 1:
Python3
# 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()
Python3
# 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()
输出:
示例 2:
Python3
# 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()
输出: