你如何强制 python 子图具有相等的宽度和单独的高度(gridspec 不起作用)? [英] How do you force python subplots to have equal widths and separate heights (gridspec isn't working)?

查看:39
本文介绍了你如何强制 python 子图具有相等的宽度和单独的高度(gridspec 不起作用)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下图.要求每个轴的纵横比相等.我喜欢子图之间的宽度匹配和由子图的纵横比定义的高度.

I have the following figure. Equal aspect ratio for each of the axes is a requirement. I'd love the widths to match between subplots and the height to be defined by the aspect ratio of the subplot.

我尝试使用 GridSpec- 一团糟- 缩小空白以制作合理的图是不可能的,所以我一定做错了:

I attempted using GridSpec- total mess- it's impossible to shrink the whitespace to make a reasonable plot, so I've got to be doing something wrong:

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

推荐答案

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

This is what I do when I want different height sub-plots that (usually, in my case) share the x axis:

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

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

plt.subplots_adjust(hspace=0.02) # etc

plt.subplots_adjust(hspace=0.02) # etc

这篇关于你如何强制 python 子图具有相等的宽度和单独的高度(gridspec 不起作用)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆