matplotlib 子图没有间隙但最后一个

matplotlib subplot without gaps but the last one

我想弄清楚如何才能达到标题主题中报告的结果,基本上我希望获得一个子图 1 列 4 行,第 1、2、3 行没有间隙,并且之间有正常间隙1,2,3 块和四个 现在我刚刚使用 subplot_adjust 获得了一个没有间隙的 4 行图,如下所示:

fig.subplots_adjust(wspace=0, hspace=0)

这里是完整的代码:

fig,axs = SansItalic(**{'lines.linewidth': 3} )(4,1,(14.0,14.0))


axs[0].plot(a1,a2,label='45 60',color='C5')   # ,linestyle=(0, (1, 1)))
axs[0].plot(a13,a14,label='57 75',color='C4') #,linestyle=(0, (1, 1)))

axs[1].plot(a11,a12,label='55 70',color='C3')#,linestyle=(0, (1, 1)))
axs[1].plot(a3,a4,label='60 80', color='C2')

axs[2].plot(a5,a6,label='70 90',color='C0')
#axs[2].plot(a7,a8,label='85 106',color='C1')
axs[2].plot(a9,a10,label='90 110',color='C1')
  fig.subplots_adjust(wspace=0, hspace=0)
  axs[0].plot(a1,a2,label='45 60',color='C5')  
  axs[0].plot(a13,a14,label='57 75',color='C4')

  axs[1].plot(a11,a12,label='55 70',color='C3')
axs[1].plot(a3,a4,label='60 80', color='C2')

axs[2].plot(a5,a6,label='70 90',color='C0')
#axs[2].plot(a7,a8,label='85 106',color='C1')
axs[2].plot(a9,a10,label='90 110',color='C1')
fig.subplots_adjust(wspace=0, hspace=0)

axs[3].plot(b1,b2,label='45 60', color='C5')
axs[3].plot(b3,b4,label='60 80', color='C2')
axs[3].plot(b11,b12,label='55 70', color='C3')
axs[3].plot(b13,b14,label='57 75',color='C4')
axs[3].plot(b5,b6,label='70 90', color='C0')
axs[3].set(ylabel = 'Re$_\lambda$')



  axs[0].set_title('m=3e7 - Polymer length', y=1.05)


  fig.subplots_adjust(wspace=0, hspace=0)

   axs[0].set_xlim([0,19])
   axs[1].set_xlim([0,19])
   axs[2].set_xlim([0,19])
   axs[3].set_xlim([0,19])
   axs[0].legend()

   axs[1].legend()
   axs[2].legend()
   axs[3].legend()
   plt.savefig('plot_sp_m3e7.pdf')
   plt.show()

其中 SansItalic 是一个简单的 class 自己完成的,其中我只定义了字体和一些 rc-parameters 使用此代码我获得了没有 space 的四个子图,但我想在最后有一个正常的差距 这是我现在得到的:

你可以解决这个问题的方法是插入另一个子图,然后调整高度比。让我们从结果开始吧;

这是通过以下方式实现的:

import matplotlib.pyplot as plt


gs = dict(\
         height_ratios = [1, 1, 1, .2, 1]
         )

fig, ax = plt.subplots(5, 1, gridspec_kw = gs)
fig.subplots_adjust(hspace = 0)
ax[3].axis('off')
fig.show()

您需要调整比率以达到预期的结果,但该方法可用于实现您想要的结果。

替代 is to chain two Gridspec definitions. See here for more information 关于 GridSpec 提供的可能性。

outer_gs = matplotlib.gridspec.GridSpec(2,1, height_ratios=[3,1], hspace=0.2)
inner_gs = matplotlib.gridspec.GridSpecFromSubplotSpec(3,1, subplot_spec=gs0[0], hspace=0)

fig = plt.figure()
ax1 = fig.add_subplot(inner_gs[0])
ax2 = fig.add_subplot(inner_gs[1])
ax3 = fig.add_subplot(inner_gs[2])
ax4 = fig.add_subplot(outer_gs[1])