无法仅使用 sns.heatmap 复制聚类图图

Can't replicate clustermap plot using just sns.heatmap

提问人:athantas 提问时间:11/1/2023 最后编辑:athantas 更新时间:11/3/2023 访问量:51

问:

给定一个链接矩阵,我从中得到的热图与我使用的热图不同。Zg = sns.clustermap(corr)sns.heatmap(corr[np.ix_(g.dendrogram_row.reordered_ind, g.dendrogram_row.reordered_ind)])

使用以下函数的此问题的最小示例是:

from scipy.cluster.hierarchy import dendrogram
def get_linkage_matrix(children, n_leaves, distances): #modified from sklearn
    # Create linkage matrix
    # create the counts of samples under each node
    counts = np.zeros(children.shape[0])
    n_samples = n_leaves
    for i, merge in enumerate(children):
        current_count = 0
        for child_idx in merge:
            if child_idx < n_samples:
                current_count += 1  # leaf node
            else:
                current_count += counts[child_idx - n_samples]
        counts[i] = current_count

    linkage_matrix = np.column_stack(
        [children, distances, counts]
    ).astype(float)
    return linkage_matrix

np.random.seed(0)
toy_data = np.random.randint(0,5,(5,10))
children, n_connected_components, n_leaves, parents, distances = sklearn.cluster.ward_tree(toy_data.T, return_distance=True)
Z = get_linkage_matrix(children, n_leaves, distances) #using basically plot_dendrogram function from https://scikit-learn.org/stable/auto_examples/cluster/plot_agglomerative_dendrogram.html
plt.figure(figsize=(4,3))
R = dendrogram(Z)

有了这个输出:ward dendrogram

所以,然后我尝试用相同的树状图得到一个漂亮的图:sns.clustermap

corr_toy = np.corrcoef(toy_data.T)
cmap = sns.diverging_palette(230, 20, as_cmap=True) 
g = sns.clustermap(corr_toy, method='ward',
                   vmax=.3, center=0, cmap=cmap,
                   row_linkage=Z, row_cluster=True, col_cluster=False,
                   linewidths=.5, cbar_kws={"shrink": .5}, row_colors=[R["leaves_color_list"][i] for i in R["leaves"]],
                   yticklabels=False, xticklabels=R["leaves"],
                   cbar_pos=(0.9, 0.3, 0.02, 0.3), figsize=(4,3))#nfeatures_df.iloc[:,R["leaves"]].columns)
mask = np.triu(np.ones_like(corr_toy, dtype=bool))
values = g.ax_heatmap.collections[0].get_array().reshape(corr_toy.shape)
new_values = np.ma.array(values, mask=mask)
g.ax_heatmap.collections[0].set_array(new_values)
plt.show()

有了这个输出:sns.clustermap

在这里,我已经期望我在 y 轴上显示的图形按照第一个图中树状图的颜色顺序排列,即使:row_colors=[R["leaves_color_list"][i] for i in R["leaves"]]

print(R["leaves"] == g.dendrogram_row.reordered_ind)
>>> True

通过目视检查,人们还可以验证两个树状图是否匹配。所以,这基本上是我无法解决的第一个问题,任何帮助将不胜感激。
其次,当我尝试使用并适当地对行进行排序时,即:
sns.heatmapg.dendrogram_row.reordered_ind

plt.figure(figsize=(4,3))
mask = np.triu(np.ones_like(corr_toy, dtype=bool))
cmap = sns.diverging_palette(230, 20, as_cmap=True)
sns.heatmap(corr_toy[np.ix_(g.dendrogram_row.reordered_ind, g.dendrogram_row.reordered_ind)], mask=mask,
            cmap=cmap, vmax=.3, center=0,
            square=True, linewidths=.5, cbar_kws={"shrink": .5},
            xticklabels=g.dendrogram_row.reordered_ind, yticklabels=g.dendrogram_row.reordered_ind)

我得到这个输出:sns.heatmap
这显然无法复制上面的情节,我不明白为什么。
sns.clustermap

python seaborn 热图 分层聚类 clustermap

评论

2赞 D.L 11/1/2023
该问题需要足够的代码才能获得最小的可重现示例
0赞 athantas 11/2/2023
也许现在好多了?

答: 暂无答案