通过matplotlib中的许多子图来改善子图大小/间距 [英] Improve subplot size/spacing with many subplots in matplotlib

查看:265
本文介绍了通过matplotlib中的许多子图来改善子图大小/间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题非常相似但是不同的是,我的身材可以根据需要而变大.

Very similar to this question but with the difference that my figure can be as large as it needs to be.

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

I need to generate a whole bunch of vertically-stacked plots in matplotlib. The result will be saved using figsave and viewed on a webpage, so I don't care how tall the final image is as long as the subplots are spaced so they don't overlap.

无论我允许人物多大,子图似乎总是重叠的.

No matter how big I allow the figure to be, the subplots always seem to overlap.

我的代码当前看起来像

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)

推荐答案

尝试使用 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()


没有紧凑的布局


Without Tight Layout

布局紧凑

这篇关于通过matplotlib中的许多子图来改善子图大小/间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆