Python中的 Matplotlib.axes.Axes.plot_date()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.plot_date()函数
matplotlib 库的 axes 模块中的Axes.plot_date()函数用于绘制包含日期的数据。
Syntax: Axes.plot_date(self, x, y, fmt=’o’, tz=None, xdate=True, ydate=False, *, data=None, **kwargs)
Parameters: This method accept the following parameters that are described below:
- x, y: These parameter are the horizontal and vertical coordinates of the data points.
- fmt: This parameter is an optional parameter and it contains the string value.
- tz: This parameter is the time zone to use in labeling dates.It contain timezone string.
- xdate: This parameter is also an optional parameter. And it contain boolean values with default value True. If True, the x-axis will be interpreted as Matplotlib dates.
- ydate: This parameter is also an optional parameter. And it contain boolean values with default value True. If True, the y-axis will be interpreted as Matplotlib dates.
Returns: This returns the following:
- lines:This returns the list of Line2D objects representing the plotted data.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.plot_date()函数:
示例 1:
# Implementation of matplotlib function
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import drange
import numpy as np
date1 = datetime.datetime(2020, 4, 2)
date2 = datetime.datetime(2020, 4, 6)
delta = datetime.timedelta(hours = 24)
dates = drange(date1, date2, delta)
y = np.arange(len(dates))
fig, ax = plt.subplots()
ax.plot_date(dates, y ** 2)
ax.set_title('matplotlib.axes.Axes.plot_date Example1')
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
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)
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(DateFormatter('%Y-%m-%d'))
ax.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S')
fig.autofmt_xdate()
ax.set_title('matplotlib.axes.Axes.plot_date Example2')
plt.show()
输出: