SOK
SOK

Reputation: 1792

How to adjust height of individual sublots in seaborn heatmap

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!

Upvotes: 0

Views: 255

Answers (1)

nav610
nav610

Reputation: 791

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.

Upvotes: 1

Related Questions