gpt4 book ai didi

python - pyplot 根据变化率在两条曲线之间填充

转载 作者:太空宇宙 更新时间:2023-11-04 02:42:41 25 4
gpt4 key购买 nike

我想展示两个时间序列及其变化率相互重叠的时期。我使用下面的代码,但是 fill_between 不能完全填充两条曲线之间的区域。我不知道为什么。

结果图像:

Resulting image

plt.figure(figsize=(18,12))
ax1 = plt.subplot2grid((1,1), (0,0))
ax1.plot_date(data.index, data.Net,'g-', label='Net')
ax1.plot_date(data.index, data.HS300_NET,'r-', label='HS300_Net')
ax1.fill_between(data.index, data.Net, data.HS300_NET,
where=(data.Net.pct_change() < data.HS300_NET.pct_change()),
facecolor='g', alpha=0.5)
ax1.fill_between(data.index, data.Net, data.HS300_NET,
where=(data.Net.pct_change() > data.HS300_NET.pct_change()),
facecolor='r', alpha=0.5)

plt.legend()
plt.show()

最佳答案

尝试将 interpolate=True 添加到 fill_between 调用中。

参见 example from official doc here

相关代码为

# now fill between y1 and y2 where a logical condition is met.  Note
# this is different than calling
# fill_between(x[where], y1[where],y2[where]
# because of edge effects over multiple contiguous regions.
fig, (ax, ax1) = plt.subplots(2, 1, sharex=True)
ax.plot(x, y1, x, y2, color='black')
ax.fill_between(x, y1, y2, where=y2 >= y1, facecolor='green', interpolate=True)
ax.fill_between(x, y1, y2, where=y2 <= y1, facecolor='red', interpolate=True)
ax.set_title('fill between where')

关于python - pyplot 根据变化率在两条曲线之间填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46029862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com