How to adjust height of individual sublots in seaborn heatmap

故事扮演 提交于 2020-08-10 19:09:34

问题


I have a heatmap using seaborn and am trying to adjust the height of the 4th plot below. You will see that it only has 2 rows of data vs the others that have more:

I have used the following code to create the plot:

f, ax = plt.subplots(nrows=4,figsize=(20,10))
cmap = plt.cm.GnBu_r
sns.heatmap(df,cbar=False,cmap=cmap,ax=ax[0])
sns.heatmap(df2,cbar=False,cmap=cmap,ax=ax[1])
sns.heatmap(df3,cbar=False,cmap=cmap,ax=ax[2])
sns.heatmap(df4,cbar=False,cmap=cmap,ax=ax[3])

Does anyone know the next step to essentially make the 4th plot smaller in height and thus stretching out the other 3? The 4th plot will generally always have 2-3 where as the others will have 6-7 most times. Thanks very much!


回答1:


As normal, it is pretty funky/tedious with matplotlib. But here it is!

f = plt.figure(constrained_layout = True)  
specs = f.add_gridspec(ncols = 1, nrows = 4, height_ratios = [1,1,1,.5])

for spec, df in zip(specs, (df, df2, df3, df4)):
  ax = sns.heatmap(df,cbar=False,cmap=cmap, ax=f.add_subplot(spec)) 

You can change the heights relative to each other using the height_ratios. You could also implement a wdith_ratios parameter if you desired to change the relative widths. You could also implement a for loop to iterate over the graphing.



来源:https://stackoverflow.com/questions/62887293/how-to-adjust-height-of-individual-sublots-in-seaborn-heatmap

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