2017-09-04 64 views
1

我想要显示两个时间序列和它们的变化率彼此超过的周期。我使用下面的代码,但fill_between不能完全填充两条曲线之间的区域。我不知道为什么。pyplot根据其变化率在两条曲线之间填充

产生的图像:

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() 

回答

1

尝试添加interpolate=Truefill_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')