Python 如何收紧我的';matplotlib';数字更接近我的坐标轴?
当Python 如何收紧我的';matplotlib';数字更接近我的坐标轴?,python,matplotlib,bounds,figure,axes,Python,Matplotlib,Bounds,Figure,Axes,当matplotlib制作图形时,我发现它“填充”轴周围的空间太多,不合我的口味(而且是以不对称的方式)。例如 import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) x, y = 12*np.random.rand(2, 1000) ax.set(xlim=[2,10]) ax.plot(x, y, 'go') 我得到的东西看起来像 (此处以Adobe
matplotlib
制作图形时,我发现它“填充”轴周围的空间太多,不合我的口味(而且是以不对称的方式)。例如
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x, y = 12*np.random.rand(2, 1000)
ax.set(xlim=[2,10])
ax.plot(x, y, 'go')
我得到的东西看起来像
(此处以Adobe Illustrator为例)
我希望图形的边界更靠近所有边上的轴,尤其是在左侧和右侧
如何在matplotlib
中相对于每个轴以编程方式调整这些边界?尝试:
plt.tight_layout()
默认参数集为:
plt.tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None)
您可以使用add_轴并手动指定边界,[0,0,1,1]将完全覆盖图形。以后可以使用fig.subplot_adjust()进行调整。这与使用
savefig('test.png',bbox_inches='tight')
@Raxacoricfallapatorius-效果类似,但是使用bbox\u inches='tight'
会自动选择要保存的图形的子区域,而tight\u layout
会更改图形的布局。换句话说,如果要调用plt.show()
,tight\u layout
会更改交互显示的内容,而使用bbox\u inches=“tight”保存FIG
不会。