如何在 Matplotlib 中扩展图形底部的边距?

How do I extend the margin at the bottom of a figure in Matplotlib?

下面的截图显示了我的x-axis。

我添加了一些标签并将它们旋转了 90 度以便更好地阅读它们。但是,pyplot 截断了底部,以至于我无法完全阅读标签。 如何扩展底部边距才能看到完整的标签?

两种追溯方式:

fig, ax = plt.subplots()
# ...
fig.tight_layout()

fig.subplots_adjust(bottom=0.2) # or whatever

这是一个 subplots_adjust 示例:http://matplotlib.org/examples/pylab_examples/subplots_adjust.html

(但我更喜欢tight_layout)

Subplot-adjust 对我不起作用,因为整个图形只会调整大小,标签仍然超出范围。

我发现的一个解决方法是使 y 轴始终与最高或最小 y 值保持一定的余量:

x1,x2,y1,y2 = plt.axis()
plt.axis((x1,x2,y1 - 100 ,y2 + 100))

一个对我有用的快速单行解决方案是直接使用 pyplot 的自动 tight_layout method,在 Matplotlib v1.1 及更高版本中可用:

plt.tight_layout()

可以在 显示绘图 (plt.show()) 之前立即调用,但 对坐标轴的操作之后(例如滴答标签旋转等)。

这种方便的方法避免了操纵子图的单个数字。

其中 plt 是标准 pyplot 来自: import matplotlib.pyplot as plt

fig.savefig('name.png', bbox_inches='tight')

对我来说效果最好,因为与

相比它不会减小地块大小
fig.tight_layout()