Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何收紧我的';matplotlib';数字更接近我的坐标轴?_Python_Matplotlib_Bounds_Figure_Axes - Fatal编程技术网

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
不会。