我需要在 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)
原文由 mcstrother 发布,翻译遵循 CC BY-SA 4.0 许可协议
请查看 matplotlib: Tight Layout guide 并尝试使用
matplotlib.pyplot.tight_layout
或matplotlib.figure.Figure.tight_layout
举个简单的例子:
没有紧凑的布局
布局紧凑