使用许多子图改进子图大小/间距

新手上路,请多包涵

我需要在 matplotlib 中生成一大堆垂直堆叠的图。结果将使用 savefig 保存并在网页上查看,所以我不在乎最终图像有多高,只要子图之间有间距,它们不会重叠。

无论我允许这个数字有多大,子图似乎总是重叠。

我的代码目前看起来像

import matplotlib.pyplot as plt
import my_other_module

titles, x_lists, y_lists = my_other_module.get_data()

fig = plt.figure(figsize=(10,60))
for i, y_list in enumerate(y_lists):
    plt.subplot(len(titles), 1, i)
    plt.xlabel("Some X label")
    plt.ylabel("Some Y label")
    plt.title(titles[i])
    plt.plot(x_lists[i],y_list)
fig.savefig('out.png', dpi=100)

原文由 mcstrother 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 713
2 个回答

您可以使用 plt.subplots_adjust 更改子图之间的间距。

调用签名:

 subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

参数含义(和建议的默认值)是:

 left  = 0.125  # the left side of the subplots of the figure
right = 0.9    # the right side of the subplots of the figure
bottom = 0.1   # the bottom of the subplots of the figure
top = 0.9      # the top of the subplots of the figure
wspace = 0.2   # the amount of width reserved for blank space between subplots
hspace = 0.2   # the amount of height reserved for white space between subplots

实际默认值由 rc 文件控制

原文由 CyanRook 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题