使用 seaborn.clustermap 后的 Seaborn Clustermap col_color

问题描述 投票:0回答:0

我想创建一个带有注释条的聚类图。我知道如何设置 col_colors 但是我打电话

cg = sns.clustermap(data=df, method='ward', metric='euclidean', cmap='YlOrRd', vmin=8, xticklabels=True, yticklabels=True, row_colors=col_colors, figsize=(30,30))
来自原始 DataFrame 的数据将被重新排序。我现在想使用
colors = ['blue' if 'A' in x.get_text() else 'red' for x in cg.ax_heatmap.xaxis.get_majorticklabels()]
从 cg 中提取列标签,然后将 col_color 设置为颜色。但这是不可能的。

我已经尝试过

cg.ax_col_dendrogram.bar
但结果就是这张照片:

以下是我的完整代码:

import seaborn as sns

col_colors = ['red' if 'A' in x else 'blue' for x in df.index]
cg = sns.clustermap(data=df, method='ward', metric='euclidean', cmap='YlOrRd', vmin=8, xticklabels=True, yticklabels=True, row_colors=col_colors, figsize=(30,30))

cg.ax_row_dendrogram.set_visible(False)

cg.ax_heatmap.tick_params(axis='x', labelsize=10)
cg.ax_heatmap.tick_params(axis='y', labelsize=10)

colors = ['blue' if 'A' in x.get_text() else 'red' for x in cg.ax_heatmap.xaxis.get_majorticklabels()]

for i, c in enumerate(colors):
    a = cg.ax_col_dendrogram.bar(x = (i, i), height=5, width=1, color=c, linewidth=0)
python seaborn cluster-computing clustermap
© www.soinside.com 2019 - 2024. All rights reserved.