Python 当紧密布局失败时,如何减少水平子地块间距?

Python 当紧密布局失败时,如何减少水平子地块间距?,python,matplotlib,plot,subplot,Python,Matplotlib,Plot,Subplot,我正在尝试使用subplot2grid在matplotlib中创建一个具有不同行和列跨距的多个子图 例如,以下代码: import matplotlib.pyplot as plt import numpy as n fig = plt.figure() ax1 = plt.subplot2grid((2, 5), (0, 0), rowspan=2, colspan=2) ax2 = plt.subplot2grid((2, 5), (0, 2)) ax2_2 = plt.subplot2g

我正在尝试使用subplot2grid在matplotlib中创建一个具有不同行和列跨距的多个子图

例如,以下代码:

import matplotlib.pyplot as plt
import numpy as n

fig = plt.figure()
ax1 = plt.subplot2grid((2, 5), (0, 0), rowspan=2, colspan=2)
ax2 = plt.subplot2grid((2, 5), (0, 2))
ax2_2 = plt.subplot2grid((2, 5), (0, 3))
ax2_3 = plt.subplot2grid((2, 5), (0, 4))
ax3 = plt.subplot2grid((2, 5), (1, 2), colspan=3) #, sharex=ax2)

xs = n.linspace(0, 2*n.pi, 100)
ax1.plot(xs, n.sin(xs))
ax2.plot(xs, n.cos(xs))
ax2_2.plot(xs, n.tan(xs))
ax2_3.plot(xs, n.sin(xs))
ax3.plot(xs, n.cos(xs))


ax2.set_xticklabels([])
ax2.set_yticklabels([])
ax2_2.set_xticklabels([])
ax2_2.set_yticklabels([])
ax2_3.set_xticklabels([])
ax2_3.set_yticklabels([])

# This comes from a separate attempt to make the ticks invisible, but seems to make no change.
plt.setp(ax2.get_xticklabels(), visible=False)
plt.setp(ax2.get_yticklabels(), visible=False)
plt.setp(ax2_2.get_xticklabels(), visible=False)
plt.setp(ax2_2.get_yticklabels(), visible=False)
plt.setp(ax2_3.get_xticklabels(), visible=False)
plt.setp(ax2_3.get_yticklabels(), visible=False)


fig.set_size_inches(6.5, 2.85)
fig.tight_layout()
# I tried several things here - subplots_adjust improves the vertical spacing a little, but not the horizontal.
fig.subplots_adjust(hspace=0.1)

fig.savefig('test.png', dpi=100)
这使得一个数字像:

问题在于右上角的3个单细胞子地块,每个地块的宽度仅为合适宽度的70%。紧凑的布局很好地解决了间距问题,但我不知道为什么这个空间没有被填满,或者如何绕过它


那么…有人知道如何做到这一点,使子地块占据全部可用宽度吗?

当使用
subplots\u adjust()
时,
hspace
参数将调整垂直空间,
wspace
参数将调整水平空间,因此您可能需要的是:

fig.subplots_adjust(wspace=0.1)

这会影响子图,但会同时调整所有子图的间距,因此右上角的3个图是正确的,但左和右之间的轴标签和记号标签现在重叠。它给了我们一个很好的提示去尝试,所以谢谢你@恶劣为了避免这种重叠,您可以增加:
ax1.yaxis.labelpad