python - 子图未显示在图中 - pyplots

标签 python pandas matplotlib

我最近升级了我的 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/

相关文章:

python - 绘制目录中的所有数据文件

python-3.x - 删除 Candlestick_ohlc 图表中两个日期之间的空白

python - 从 Most Simply 中清理 A String

python - 如何在 Blender 中以编程方式创建逻辑 block ?

python - 如何读取和解析二进制文件作为 Big Endian

python - 将空行移动到数据框的底部

python - 快速 Pandas 过滤

python - 预提交安装在哪里 "environments"?

Python 'map' 函数插入 NaN,是否可以返回原始值?

python - ValueError : view limit minimum -5. 1000000000000005 小于 1 并且是无效的 Matplotlib 日期值