带有seaborn clustermap bug的下三角掩码

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

要重现该错误,您可以使用此数据框(使用

.to_clipboard
对象的
pandas.DataFrame
方法制作,以便您可以轻松导入它)。

    01:01   01:02   01:03   01:04   01:05   01:06   01:07   01:10   01:12   01:21   02:01   03:01   03:02   03:03   04:01   04:02   04:04   05:01   05:02   05:03   05:05   05:08   05:09   05:11   05:13   06:01
01:01   0   0   0   1   1   0   1   0   1   0   4   4   4   4   5   5   5   6   6   6   6   6   6   6   6   5
01:02   0   0   0   1   1   0   1   0   1   0   4   4   4   4   5   5   5   6   6   6   6   6   6   6   6   5
01:03   0   0   0   1   1   0   1   0   1   0   4   4   4   4   5   5   5   6   6   6   6   6   6   6   6   5
01:04   1   1   1   0   0   1   0   1   0   1   5   5   5   5   6   6   6   7   7   7   7   7   7   7   7   6
01:05   1   1   1   0   0   1   0   1   0   1   5   5   5   5   6   6   6   7   7   7   7   7   7   7   7   6
01:06   0   0   0   1   1   0   1   0   1   0   4   4   4   4   5   5   5   6   6   6   6   6   6   6   6   5
01:07   1   1   1   0   0   1   0   1   0   1   5   5   5   5   6   6   6   7   7   7   7   7   7   7   7   6
01:10   0   0   0   1   1   0   1   0   1   0   4   4   4   4   5   5   5   6   6   6   6   6   6   6   6   5
01:12   1   1   1   0   0   1   0   1   0   1   5   5   5   5   6   6   6   7   7   7   7   7   7   7   7   6
01:21   0   0   0   1   1   0   1   0   1   0   4   4   4   4   5   5   5   6   6   6   6   6   6   6   6   5
02:01   4   4   4   5   5   4   5   4   5   4   0   2   2   2   3   3   3   4   4   4   4   4   4   4   4   3
03:01   4   4   4   5   5   4   5   4   5   4   2   0   0   0   3   3   3   4   4   4   4   4   4   4   4   3
03:02   4   4   4   5   5   4   5   4   5   4   2   0   0   0   3   3   3   4   4   4   4   4   4   4   4   3
03:03   4   4   4   5   5   4   5   4   5   4   2   0   0   0   3   3   3   4   4   4   4   4   4   4   4   3
04:01   5   5   5   6   6   5   6   5   6   5   3   3   3   3   0   0   0   1   1   1   1   1   1   1   1   0
04:02   5   5   5   6   6   5   6   5   6   5   3   3   3   3   0   0   0   1   1   1   1   1   1   1   1   0
04:04   5   5   5   6   6   5   6   5   6   5   3   3   3   3   0   0   0   1   1   1   1   1   1   1   1   0
05:01   6   6   6   7   7   6   7   6   7   6   4   4   4   4   1   1   1   0   0   0   0   0   0   0   0   1
05:02   6   6   6   7   7   6   7   6   7   6   4   4   4   4   1   1   1   0   0   0   0   0   0   0   0   1
05:03   6   6   6   7   7   6   7   6   7   6   4   4   4   4   1   1   1   0   0   0   0   0   0   0   0   1
05:05   6   6   6   7   7   6   7   6   7   6   4   4   4   4   1   1   1   0   0   0   0   0   0   0   0   1
05:08   6   6   6   7   7   6   7   6   7   6   4   4   4   4   1   1   1   0   0   0   0   0   0   0   0   1
05:09   6   6   6   7   7   6   7   6   7   6   4   4   4   4   1   1   1   0   0   0   0   0   0   0   0   1
05:11   6   6   6   7   7   6   7   6   7   6   4   4   4   4   1   1   1   0   0   0   0   0   0   0   0   1
05:13   6   6   6   7   7   6   7   6   7   6   4   4   4   4   1   1   1   0   0   0   0   0   0   0   0   1
06:01   5   5   5   6   6   5   6   5   6   5   3   3   3   3   0   0   0   1   1   1   1   1   1   1   1   0

它是一个26x26的矩阵。 我使用以下代码从这篇SO帖子中汲取灵感(带有seaborn clustermap的下三角掩码):

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

matrix = matrix.astype(int)

# Generate a clustermap
cg = sns.clustermap(matrix, annot=True, cmap = "Blues", cbar_pos=(.09, .6, .05, .2))

# Mask the lower triangle
mask = np.tril(np.ones_like(matrix))
values = cg.ax_heatmap.collections[0].get_array().reshape(matrix.shape)
new_values = np.ma.array(values, mask=mask)

cg.ax_heatmap.collections[0].set_array(new_values)
cg.ax_row_dendrogram.set_visible(False)
cg.ax_col_dendrogram.set_visible(False)
cg.savefig("dqa_eplet_distances_abv.png", dpi=600)

但是我得到的结果:

为什么这不是我期望的三角形版本的矩阵?原始 SO 帖子中的 MRE 几乎与我的代码相同,我无法理解我哪里出错了。

我正在使用

seaborn-0.13.2
Python 3.11.1

python pandas
1个回答
0
投票

链接的代码可以很好地屏蔽值,但不适用于注释

一种选择可能是循环文本并掩盖对角线下方的文本:

for idx, t in enumerate(cg.ax_heatmap.texts):
    n = matrix.shape[0]
    if idx//n >= idx%n:
        t.set_visible(False)

© www.soinside.com 2019 - 2024. All rights reserved.