gpt4 book ai didi

python - seaborn 集群图中的附加行颜色

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:07 25 4
gpt4 key购买 nike

我目前正在 seaborn 中生成 clustermaps 并如下标记行颜色。

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 将此注释列添加到集群的解决方法。

如果我不能使用 seaborn 来执行此操作并且必须使用 matplotlib 手动生成所有这些,那也没问题,我也无法弄清楚。

感谢您的帮助!

clustermap example plot

最佳答案

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

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: Clustermap with two additional columns

关于python - seaborn 集群图中的附加行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48173798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com