import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 8 * np.pi, 1000) siny = np.sin(x) cosy = np.cos(x / 2) / 2 plt.plot(x, siny, c='skyblue', label='sin-X') plt.plot(x, cosy, c='orangered', label='1/2 cos 1/2x' ) # 填充颜色 plt.fill_between(x, cosy, siny, where=cosy<siny, color='green', ) plt.fill_between(x, cosy, siny, where=cosy>siny, color='gray', ) plt.legend() plt.show()
plt.fill_between(x=X数组, y1=Y开始区, y2=Y结束区域, where=条件(y1> y2 or y2>y1....) ,color='green', )