子图没有间隙但最后一个

子图没有间隙但最后一个

本文介绍了matplotlib 子图没有间隙但最后一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何实现标题主题中报告的结果,基本上我希望获得一个子图 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 - 聚合物长度', 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 参数我获得了没有空格的四个子图,但我想要一个正常的间隙在最后这就是我现在得到的:

解决方案

I'm try to figure out how can I achieve the result as reported in the title's topic, basically I wish obtain a subplot 1 column 4 row without gaps in the row 1,2,3 and a normal gaps between the 1,2,3 block and the fourRight now I have just obtain a 4 row plot without gaps using the subplot_adjust as follow:

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

here the complete code:

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

Where SansItalic is a simple class self done in which i just define the fonts and some rc-parameters with this code I obtain the four subplot without space, but I would like to have a normal gaps in the lastthis is what I obtain now :

解决方案

An alternative to the answer by @GlobalTraveler is to chain two Gridspec definitions. See here for more information about the possibilities offered by 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])

这篇关于matplotlib 子图没有间隙但最后一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:37