Python-通过matplotlib中的许多子图来改善子图大小/间距

发布于 2021-02-02 23:15:15

与这个问题非常相似,但不同之处在于我的身材可以达到所需的大小。

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

无论我允许多大的身材,子图似乎总是重叠的。

我的代码目前看起来像

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)
关注者
0
被浏览
212
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    试使用 plt.tight_layout

    作为一个简单的例子:

    import matplotlib.pyplot as plt
    
    fig, axes = plt.subplots(nrows=4, ncols=4)
    fig.tight_layout() # Or equivalently,  "plt.tight_layout()"
    
    plt.show()
    

    没有紧凑的布局



  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    您可以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文件控制



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看