Python中的 Matplotlib.axis.Axis.set_major_formatter()函数
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。它是Python中用于二维数组图的惊人可视化库,用于处理更广泛的 SciPy 堆栈。
matplotlib.axis.Axis.set_major_formatter()函数
matplotlib 库的轴模块中的Axis.set_major_formatter()函数用于设置主要股票代码的格式化程序。
Syntax: Axis.set_major_formatter(self, formatter)
Parameters: This method accepts the following parameters.
- formatter: This parameter is the Formatter.
Return value: This method does not returns any value.
下面的示例说明了 matplotlib.axis 中的 matplotlib.axis.Axis.set_major_formatter()函数:
示例 1:
Python3
# Implementation of matplotlib function
import numpy as np
from matplotlib.axis import Axis
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
np.random.seed(19680801)
fig, ax = plt.subplots()
ax.plot(100*np.random.rand(20))
formatter = ticker.FormatStrFormatter('?%1.2f')
Axis.set_major_formatter(ax.yaxis, formatter)
for tick in ax.yaxis.get_major_ticks():
tick.label1.set_color('green')
plt.title("Matplotlib.axis.Axis.set_major_formatter()\n\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
Python3
# 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, 'g')
ax.set_xlim(dates[0], dates[-1])
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_minor_locator(HourLocator(range(0, 25, 6)))
ax.xaxis.set_major_formatter(plt.NullFormatter())
ax.fmt_xdata = DateFormatter('% Y-% m-% d % H:% M:% S')
fig.autofmt_xdate()
plt.title("Matplotlib.axis.Axis.set_major_formatter()\n\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
输出:
示例 2:
Python3
# 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, 'g')
ax.set_xlim(dates[0], dates[-1])
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_minor_locator(HourLocator(range(0, 25, 6)))
ax.xaxis.set_major_formatter(plt.NullFormatter())
ax.fmt_xdata = DateFormatter('% Y-% m-% d % H:% M:% S')
fig.autofmt_xdate()
plt.title("Matplotlib.axis.Axis.set_major_formatter()\n\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
输出: