七月
31
matplotlib教程:区域填充
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 5*np.pi, 1000)
y1 = np.sin(x)
y2 = np.sin(2*x)
fig = plt.figure()
ax = fig.add_subplot(111)
# 一次性画多条曲线
ax.plot(x, y1, x, y2)
# # 使用fill方法填充曲线和x轴之间的区域
# ax.fill(x, y1, 'b', alpha=0.3)
# ax.fill(x, y2, 'r', alpha=0.3)
# 填充两条曲线之间的区域
# 注意这里的where表达式,表示填充的位置
# inteporlate 表示填充由于数值精度不够引起的未填充空白区域
ax.fill_between(x, y1, y2, where=y1>y2, facecolor='y', interpolate=True)
ax.fill_between(x, y1, y2, where=y1<y2, facecolor='g', interpolate=True)
fig.show()