seaborn 集群映射中的附加行颜色 [英] additional row colors in seaborn cluster map

查看:33
本文介绍了seaborn 集群映射中的附加行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 seaborn 中生成集群图,并将行颜色标记如下.

I am currently generating clustermaps in seaborn and labeling the row colors as below.

matrix = pd.DataFrame(np.random.random_integers(0,1, size=(50,4)))
labels = np.random.random_integers(0,5, size=50)

lut = dict(zip(set(labels), sns.hls_palette(len(set(labels)), l=0.5, s=0.8)))
row_colors = pd.DataFrame(labels)[0].map(lut)

g=sns.clustermap(matrix, col_cluster=False, linewidths=0.1, cmap='coolwarm', row_colors=row_colors)
plt.show()

我还有第二个注释列,类似于我也想添加到绘图中的标签数据.seaborn API 不支持添加第二个 row_colors 列,这很好,但我正在努力寻找使用 matplotlib 将此注释列添加到聚类的解决方法.

I have a second annotation column similar to the labels data I would also like to add to the plot. The seaborn API doesn't support adding a second row_colors column, which is fine, but I am struggling in finding a workaround using matplotlib to add this annotation column to the clustering.

如果我不能使用 seaborn 来做到这一点,并且必须使用 matplotlib 手动生成所有这些,那我也想不通.

If I cannot use seaborn to do this and have to generate all of this manually using matplotlib that would be fine, I just can't figure that out either.

感谢您的帮助!

推荐答案

解决方案如下.seaborn API实际上确实允许这样做.

The solution is below. The seaborn API does actually allow this to be done.

matrix = pd.DataFrame(np.random.random_integers(0,1, size=(50,4)))

labels = np.random.random_integers(0,5, size=50)
lut = dict(zip(set(labels), sns.hls_palette(len(set(labels)), l=0.5, s=0.8)))
row_colors = pd.DataFrame(labels)[0].map(lut)

#Create additional row_colors here
labels2 = np.random.random_integers(0,1, size=50)
lut2 = dict(zip(set(labels2), sns.hls_palette(len(set(labels2)), l=0.5, s=0.8)))
row_colors2 = pd.DataFrame(labels2)[0].map(lut2)

g=sns.clustermap(matrix, col_cluster=False, linewidths=0.1, cmap='coolwarm', row_colors=[row_colors, row_colors2])
plt.show()

这会生成一个带有两个附加列的 Clustermap:

This produces a Clustermap with two additional columns:

这篇关于seaborn 集群映射中的附加行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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