matplotlib.axes.axes.fill_between
matplotlib.axes.axes.fill_between()函数,matplotlib库的Axes模块中的ax.fill_between()函数用于填充两条水平曲线之间的区域。
语法:
Axes.fill_between(self, x, y1, y2=0, where=None, interpolate=False, step=None, *, data=None, **kwargs)
参数:该方法接受如下参数说明:
- x:该参数包含用于定义曲线的数据点的横坐标。
- y1:该参数包含用于定义第一条曲线的数据点的y坐标
- y2:该参数包含用于定义第二条曲线的数据点的y坐标。它是可选的,默认值为0。
- where:该参数为可选参数。并用于排除一些水平区域的填充。
- interpolate:可选参数。它是错误条的线宽,默认值为NONE。
- step:可选参数。用于确定填充是否为阶跃函数。
返回一个包含绘制多边形的PolyCollection。
下面的例子演示了matplotlib.axes.axes.fill_between()函数在matplotlib.axes中的作用:
示例1
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-5, 5, 0.01)
y1 = -3 * x*x + 10 * x + 10
y2 = 3 * x*x + x
fig, ax = plt.subplots()
ax.plot(x, y1, x, y2, color ='black')
ax.fill_between(x, y1, y2, where = y2 >y1,
facecolor ='green', alpha = 0.8)
ax.fill_between(x, y1, y2, where = y2 <= y1,
facecolor ='black', alpha = 0.8)
ax.set_title('matplotlib.axes.Axes.fill_between Example1')
plt.show()
输出:
示例2
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2 * np.pi * x)
y2 = 0.8 * np.sin(4 * np.pi * x)
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1,
sharex = True,
figsize =(6, 6))
ax1.fill_between(x, y1, facecolor ='green')
ax1.set_title('Fill_Between y1 and 0')
ax2.fill_between(x, y1, 1, facecolor ='green')
ax2.set_title('Fill_Between y1 and 1')
ax3.fill_between(x, y1, y2, facecolor ='green')
ax3.set_title('Fill_Between y1 and y2')
ax3.set_xlabel('x')
fig.tight_layout()
ax4.fill_between(x, y1, y2, where = y2 <= y1,
facecolor ='green')
ax4.set_title('Fill_Between y1 and y2 with condition y2 <= y1 ' )
ax4.set_xlabel('x')
fig.tight_layout()
plt.show()
输出: