gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:11:09 27 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/6541123/

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