Python中的 Matplotlib.axes.Axes.plot()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。
Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
例子:
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange
import numpy as np
date1 = datetime.datetime(2000, 3, 2)
date2 = datetime.datetime(2000, 3, 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()
plt.title("Matplotlib Axes Class Example")
plt.show()
输出:
matplotlib.axes.Axes.plot()函数
matplotlib 库的 axes 模块中的Axes.plot()函数用于将 y 与 x 绘制为线条和/或标记。
Syntax: Axes.plot(self, *args, scalex=True, scaley=True, 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. x values are optional.
- fmt: This parameter is an optional parameter and it contains the string value.
- data: This parameter is an optional parameter and it is an object with labelled data.
Returns: This returns the following:
- lines:This returns the list of Line2D objects representing the plotted data.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.plot()函数:
示例 #1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
# make an agg figure
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
ax.set_title('matplotlib.axes.Axes.plot() example 1')
fig.canvas.draw()
plt.show()
输出:
示例 #2:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
# create random data
xdata = np.random.random([2, 10])
# split the data into two parts
xdata1 = xdata[0, :]
xdata2 = xdata[1, :]
# sort the data so it makes clean curves
xdata1.sort()
xdata2.sort()
# create some y data points
ydata1 = xdata1 ** 2
ydata2 = 1 - xdata2 ** 3
# plot the data
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(xdata1, ydata1, color ='tab:blue')
ax.plot(xdata2, ydata2, color ='tab:orange')
# set the limits
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.set_title('matplotlib.axes.Axes.plot() example 2')
# display the plot
plt.show()
输出: