如何使用matplotlib和gridspec将一个轴与其他轴隔开?

如何使用matplotlib和gridspec将一个轴与其他轴隔开?,matplotlib,Matplotlib,我正在使用matplotlib和grispec绘制4个轴,其中三个轴我希望彼此紧密连接,最后一个轴我希望与上述三个轴间隔一点。原因是我希望前三个共享相同的x轴单位,而下一个共享不同的x轴单位。我试着在第三个轴之后使用gs.update,但这会将所有轴彼此隔开,而不是将底部第四个轴与顶部三个轴隔开 我是否缺少一个简单的gridspec/matplotlib命令,或者我是否必须设法解决这个问题 import matplotlib.pyplot as plt fig = plt.figure(fig

我正在使用matplotlib和grispec绘制4个轴,其中三个轴我希望彼此紧密连接,最后一个轴我希望与上述三个轴间隔一点。原因是我希望前三个共享相同的x轴单位,而下一个共享不同的x轴单位。我试着在第三个轴之后使用
gs.update
,但这会将所有轴彼此隔开,而不是将底部第四个轴与顶部三个轴隔开

我是否缺少一个简单的gridspec/matplotlib命令,或者我是否必须设法解决这个问题

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(13,12))
gs1=fig.add_gridspec(nrows=4, ncols=2, hspace=0.0)

ax2=fig.add_subplot(gs1[1,:])
ax1=fig.add_subplot(gs1[0,:], sharey=ax2)
ax3=fig.add_subplot(gs1[2,:], sharey=ax2)

ax4=fig.add_subplot(gs1[3,:]) #<- want this spaced farther down than the above three axes

plt.show()
导入matplotlib.pyplot作为plt
图=plt.图(图尺寸=(13,12))
gs1=fig.add\u gridspec(nrows=4,ncols=2,hspace=0.0)
ax2=图add_子批次(gs1[1,:])
ax1=图add_子批次(gs1[0,:],sharey=ax2)
ax3=图add_子批次(gs1[2,:],sharey=ax2)
ax4=fig.add_子图(gs1[3,:])#这是一个解决方案——这里我首先为前三个图添加了gridspec。我指定的应该在距离底部0.35处结束(0是最底部,1是顶部)。然后我添加了另一个gridspec,它从0.3开始,因此有一个0.05的间隙。显然,你可以在数字/位置上玩

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(13,12))
gs1=fig.add_gridspec(nrows=3, ncols=1, hspace=0, bottom=0.35)

ax1=fig.add_subplot(gs1[0,0])
ax2=fig.add_subplot(gs1[1,0], sharey=ax1)
ax3=fig.add_subplot(gs1[2,0], sharey=ax1)

gs2=fig.add_gridspec(nrows=1, ncols=1, top=0.3)
ax4=fig.add_subplot(gs2[0,0])

for ax in fig.axes:
    ax.set_yticks([])
    ax.set_xticks([])

plt.show()
结果:


一个受约束布局的解决方案是将艺术家放在为您创造空间的轴线上。这可能是标题。也可以是与背景颜色相同的补丁。