Python中的 matplotlib.axes.Axes.stackplot()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.stackplot()函数
matplotlib 库的 axes 模块中的Axes.stackplot()函数用于创建堆叠区域 plo。
Syntax: Axes.stackplot(axes, x, *args, labels=(), colors=None, baseline=’zero’, data=None, **kwargs)
Parameters: This method accept the following parameters that are described below:
- x: This parameter is the sequence of x coordinates.
- y: This parameter is the sequence of y coordinates.
- baseline: This parameter is the base line{‘zero’, ‘sym’, ‘wiggle’, ‘weighted_wiggle’}.
- colors: This parameter is the list or tuple of colors.
- label: This parameter is the label to assign to each data series.
Returns: This returns the following:
- list: This returns the list of PolyCollection instances, one for each element in the stacked area plot.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.stackplot()函数:
示例 1:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y1 = [1, 1, 2, 3, 5]
y2 = [0, 4, 2, 6, 8]
y3 = [1, 3, 5, 7, 9]
y = np.vstack([y1, y2, y3])
labels = ["Geeks1 ", "Geeks2", "Geeks3"]
fig, ax = plt.subplots()
ax.stackplot(x, y1, y2, y3,
labels = labels)
ax.legend(loc ='upper left')
ax.set_title('matplotlib.axes.Axes.stackplot Example')
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
def GFG(n, m):
def geeks(a):
x = 1 / (.1 + np.random.random())
y = 2 * np.random.random() - .5
z = 10 / (.1 + np.random.random())
for i in range(m):
w = (i / m - y) * z
a[i] += x * np.exp(-w * w)
a = np.zeros((m, n))
for i in range(n):
for j in range(5):
geeks(a[:, i])
return a
test = GFG(3, 100)
fig, ax = plt.subplots()
ax.stackplot(range(100), test.T,
baseline ='wiggle')
ax.set_title('matplotlib.axes.Axes.stackplot Example')
plt.show()