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