import matplotlib.pyplot as plt import numpy as np x = np.arange(0.0, 2, 0.01) y1 = np.sin(2 * np.pi * x) y2 = 1.2 * np.sin(4 * np.pi * x) fig = plt.figure() ax = plt.gca() ax.plot(x, y1, x, y2, color='black') # 填充两个轮廓线之间的区域 ''' where:指定一个条件来填充曲线,where参数接受布尔值(可以是表达式) interpolate:自动填充空白区域,当x取值比较离散时,两个轮廓之间的区域可能有空白存在 另外的方法有fill_betweenx(),针对水平曲线; 还有更通用的fill()方法,对任意多边形填充颜色或者阴影线 ''' ax.fill_between(x, y1, y2, where=y2 >= y1, facecolors='darkblue', interpolate=True) ax.fill_between(x, y1, y2, where=y2 <= y1, facecolors='deeppink', interpolate=True) ax.set_title('filled between') plt.show()