我最近升级了我的 pandas python 和我以前工作的 pyplots 程序不会以同样的方式处理子图。之前的脚本在一个人物上有三个图表堆叠在一起。现在,当它运行时,只显示第三个图表(在屏幕的底部 1/3)。所以我即将进行调试,但缺少一个步骤。有人可以建议吗?谢谢。
f 是一个 df:
Date Open High Low Close Volume Adj_Close
2011-01-03 15.82 16.200001 15.78 15.8 41760200 15.014473
2011-01-04 16.450001 16.59 16.209999 16.52 75576400 15.698677
2011-01-05 16.34 16.59 16.110001 16.559999 48278700 15.736688
2011-01-06 16.719999 16.719999 16.23 16.360001 37700800 15.546632
2011-01-07 16.450001 16.469999 16.1 16.42 36302500 15.603649
代码:
ticker = 'XYZ'
plt.subplot(3, 1, 1)
f.plot(y= ['Adj_Close','Open'], title= ticker + " Close & Open")
plt.subplot(3, 1, 2)
f.plot(y= ['High', 'Low'], title='High and Low')
ax = plt.subplot(3, 1, 3)
ax.bar(f.index, f['Volume'], width = 1)
ax.set_title("Volume")
plt.gcf().set_size_inches(18.5, 18.5)
plt.tight_layout()
date_today = datetime.date.today().isoformat()
filename = '/data/file/' + ticker + "_" + date_today + ".png"
plt.gcf().savefig(filename)
plt.close()
再次感谢
最佳答案
我相信这是因为您没有为前两个图分配轴。尝试这样的事情:
fig, (ax1,ax2,ax3) = plt.subplots(3,1)
f.plot(y= ['Adj_Close','Open'], title= ticker + " Close & Open", ax=ax1)
f.plot(y= ['High', 'Low'], title='High and Low', ax=ax2)
ax3.bar(f.index, f['Volume'], width = 1)
plt.tight_layout()
以便每个图都明确分配给特定的轴对象。
关于python - 子图未显示在图中 - pyplots,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32504571/