你如何强制python子图具有相等的宽度和单独的高度(gridspec不工作)?

时间:2015-06-02 18:23:53

标签: python matplotlib plot

我有下图。每个轴的相等纵横比是必需的。我喜欢在子图之间匹配的宽度和由子图的纵横比定义的高度。 attempt1_subplot

我尝试使用GridSpec-总乱 - 不可能缩小空白以制作合理的情节,所以我必须做错事: attempt2_gridspec

left  = 0.125  
right = 0.9    
bottom = 0.2   
top = 0.9      
wspace = 0.1  
hspace = 0.1   

fig = plt.figure(5, figsize=(4, 12))
gs = gridspec.GridSpec(5, 1,
                   width_ratios=[1,1,1,1,1],
                   height_ratios=[0.5,1.2,1,1.5,3])
plt.subplots_adjust(left=left,right=right,wspace=wspace,bottom=bottom,top=top,hspace=hspace)

ax1 = plt.subplot(gs[0],aspect='equal',xlim=[0,2],ylim=[0,0.5])
plt.plot(z,z,color='black',zorder=1)                                              
ax1.grid(which='major', alpha=0.5)
sc = plt.scatter(bob1[:-1,4],bob1[:-1,2],cmap=cmap,c=z,s=45,zorder=2)

ax2 = plt.subplot(gs[1],aspect='equal',xlim=[0,1.5],ylim=[0,0.8])
plt.plot(z,z,color='black',zorder=1)                                              
ax2.grid(which='major', alpha=0.5)
major_ticks = np.arange(0, 1.4, 0.4)                                               
ax2.set_xticks(major_ticks)
ax2.set_yticks(major_ticks)
sc = plt.scatter(bob2[:-1,4],bob2[:-1,2],cmap=cmap,c=z,s=45,zorder=2)

ax3 = plt.subplot(gs[2],aspect='equal',xlim=[0,2],ylim=[0,1])
plt.plot(z,z,color='black',zorder=1)                                               
ax3.grid(which='major', alpha=0.5)
sc = plt.scatter(bob3[:-1,4],bob3[:-1,2],cmap=cmap,c=z,s=45,zorder=2)

ax4 = plt.subplot(gs[3],aspect='equal',xlim=[0,2],ylim=[0,1.5],zorder=2)
plt.plot(z,z,color='black',zorder=1)                                              
ax4.grid(which='major', alpha=0.5)
sc = plt.scatter(bob4[:-1,4],bob4[:-1,2],cmap=cmap,c=z,s=45,zorder=2)

ax5 = plt.subplot(gs[4],aspect='equal',xlim=[0,3],ylim=[0,3])
plt.plot(z,z,color='black',zorder=1)                                             
ax5.grid(which='major', alpha=0.5)
major_ticks = np.arange(0, 3, 1.0)                                               
ax5.set_xticks(major_ticks)
ax5.set_yticks(major_ticks)
sc = plt.scatter(bob5[:-1,4],bob5[:-1,2],cmap=cmap,c=z,s=45,zorder=2)

cbar_ax = fig.add_axes([0.8, 0.3, 0.03, 0.4])
cbar = fig.colorbar(sc, cax=cbar_ax)
cbar.set_ticks([0,6.45,13])
cbar.set_ticklabels([14,7,0])

1 个答案:

答案 0 :(得分:-1)

当我想要不同的高度子图(通常在我的情况下)共享x轴时,这就是我所做的:

fig,ax = plt.subplots(nrows = 2,sharex = True,gridspec_kw = dict(height_ratios = [1,3]))

plt.subplots_adjust(hspace = 0.02)#etc