我有两组数据显示为图表中的线条。如何填充它们之间的颜色区域?import matplotlib.pyplot as pltcurve1, = plt.plot(xdata, ydata)curve2, = plt.plot(xdata, ydata)我试过:x = np.arange(0,12,0.01)plt.fill_between(x, curve1, curve2, color='yellow')谢谢
1 回答

杨魅力
TA贡献1811条经验 获得超6个赞
您必须将ydata用作 参数fill_between,而不是曲线。
要么ydata直接使用,要么从你的curve1/2对象中获取它们,比如ydata=curve1.get_ydata().
这是改编自文档的示例:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-5, 5, 0.01)
y1 = -5*x*x + x + 10
y2 = 5*x*x + x
c1, = plt.plot(x, y1, color='black')
c2, = plt.plot(x, y2, color='black')
# If you want/have to get the data form the plots
# x = c1.get_xdata()
# y1 = c1.get_ydata()
# y2 = c2.get_ydata()
plt.fill_between(x, y1, y2, where=y2 >y1, facecolor='yellow', alpha=0.5)
plt.fill_between(x, y1, y2, where=y2 <=y1, facecolor='red', alpha=0.5)
plt.title('Fill Between')
plt.show()
最后你得到:
添加回答
举报
0/150
提交
取消