gpt4 book ai didi

python - 通过许多子图改善子图大小/间距

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:06 28 4
gpt4 key购买 nike

我需要在 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)

最佳答案

请查看matplotlib: Tight Layout guide并尝试使用 matplotlib.pyplot.tight_layout ,或matplotlib.figure.Figure.tight_layout

举个简单的例子:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=4, ncols=4, figsize=(8, 8))
fig.tight_layout() # Or equivalently, "plt.tight_layout()"

plt.show()
<小时/>

没有紧凑的布局

enter image description here

<小时/>

布局紧凑

enter image description here

关于python - 通过许多子图改善子图大小/间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54539550/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com