matplotlib subplot without gaps but the last one

江枫思渺然 提交于 2019-12-04 06:41:34

问题


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 four Right 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 last this is what I obtain now :


回答1:


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




回答2:


One you could solve this is by inserting another subplot and them playing around with the height ratios. Let's start with the results;

Which is achieved by:

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

You need to play around with the ratios to achieve the desired results, but the approach could be used to achieve what you want.



来源:https://stackoverflow.com/questions/54745073/matplotlib-subplot-without-gaps-but-the-last-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!