【问题标题】:matplotlib subplot without gaps but the last onematplotlib 子图没有间隙但最后一个
【发布时间】:2019-07-11 16:58:00
【问题描述】:

我试图弄清楚如何才能达到标题主题中报告的结果,基本上我希望获得一个子图 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 是一个简单的自我完成的类,我只用这段代码定义字体和一些 rc 参数,我得到四个没有空格的子图,但我想在最后一个正常的间隙 这是我现在得到的:

【问题讨论】:

    标签: python matplotlib subplot


    【解决方案1】:

    the answer by @GlobalTraveler 的替代方法是链接两个 Gridspec 定义。请参阅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])
    

    【讨论】:

    • @GlobalTraveler 的答案与您的一样需要“手动调整”。你需要调整hspace=0.2,其他答案需要调整height_ratios列表中的数字.2
    • 好的,重点。我仍然认为我提出的解决方案更易于阅读且更灵活,尤其是在决定更改子图数量的情况下。
    • 您同样可以争辩说另一种解决方案更灵活,因为它会例如允许许多不同大小的间隙。
    【解决方案2】:

    你可以通过插入另一个子图来解决这个问题,然后他们玩弄高度比。让我们从结果开始;

    通过以下方式实现:

    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()
    

    您需要使用比率来获得所需的结果,但该方法可用于实现您想要的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 2013-10-11
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      相关资源
      最近更新 更多