使用 matplotlib 中的许多子图改进子图大小/间距 [英] Improve subplot size/spacing with many subplots in matplotlib
本文介绍了使用 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屋!
查看全文