如何调整seaborn热图中单个子批次的高度 [英] How to adjust height of individual sublots in seaborn heatmap

查看:32
本文介绍了如何调整seaborn热图中单个子批次的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 seaborn 的热图,正在尝试调整下面第 4 个图的高度.您会看到它只有 2 行数据,而其他数据行则更多:

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

有谁知道下一步要使第 4 个地块的高度变小,从而拉伸其他 3 个地块吗?第 4 个情节通常总是有 2-3 个,而其他情节最多有 6-7 个.非常感谢!

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!

推荐答案

通常情况下,matplotlib 非常时髦/乏味.但它来了!

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

您可以使用 height_ratios 更改相对于彼此的高度.如果您希望更改相对宽度,您还可以实现 wdith_ratios 参数.您还可以实现一个 for 循环来遍历图形.

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.

这篇关于如何调整seaborn热图中单个子批次的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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