为了账号安全,请及时绑定邮箱和手机立即绑定

如何填充由线连接的两组数据之间的区域?

如何填充由线连接的两组数据之间的区域?

jeck猫 2021-12-08 16:02:46
我有两组数据显示为图表中的线条。如何填充它们之间的颜色区域?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()

最后你得到:

//img1.sycdn.imooc.com//61b066c60001163c06240475.jpg

查看完整回答
反对 回复 2021-12-08
  • 1 回答
  • 0 关注
  • 233 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信