pyplot:在图的底部延伸边距

时间:2015-01-10 16:06:45

标签: matplotlib margin

以下屏幕截图显示了我的xaxis。我添加了一些标签并将它们旋转90度以便更好地读取它们。然而,pyplot截断底部,使得我无法完全读取标签。如何扩展底部边距以查看完整标签?

enter image description here

4 个答案:

答案 0 :(得分:32)

两种追溯方式:

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

答案 1 :(得分:6)

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

plt.tight_layout()

这可以在之前立即调用显示情节(plt.show()),但之后在轴上进行操作(例如,ticklabel旋转等)。

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

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

答案 2 :(得分:3)

Subplot-adjust对我来说不起作用,因为整个数字只会在标签仍然超出范围时调整大小。

我找到的解决方法是保持y轴始终比最高或最小y值保持一定的余量:

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

答案 3 :(得分:0)

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

最适合我,因为与相比,它不会减小情节大小

fig.tight_layout()