Matplotlib.axes.axes.format_xdata()
Matplotlib是Python中的一个库,它是NumPy库的数值-数学扩展。Axes包含了大多数图形元素:Axis、Tick、Line2D、Text、Polygon等,并设置坐标系。Axes的实例通过callbacks属性支持回调。
函数:Matplotlib.axes.axes.format_xdata()
matplotlib库的Axes模块中的Axes.format_xdata()函数用于返回格式化为x值的x。
语法:
Axes.format_xdata(self, x)
下面的例子演示了matplotlib.axes.axes.format_xdata()函数在matplotlib.axes中的作用:
示例1
# Implementation of matplotlib function
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")
ax.xaxis.set_major_locator(years)
ax.set_ylim((100, 300))
ax.format_xdata = mdates.DateFormatter('% m')
ax.grid(True)
fig.suptitle('matplotlib.axes.Axes.format_xdata() function\
Example', fontweight ="bold")
plt.show()
输出:
示例2
# Implementation of matplotlib function
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']
fig, ax = plt.subplots()
ax.plot('date', 'adj_close', data = data)
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(years_fmt)
ax.xaxis.set_minor_locator(months)
ax.format_xdata = mdates.DateFormatter('% Y')
ax.grid(True)
fig.autofmt_xdate()
fig.suptitle('matplotlib.axes.Axes.format_xdata() function \
Example', fontweight ="bold")
plt.show()
输出: