You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Seaborn Clustermap下三角掩码失效问题求助

问题排查:Seaborn Clustermap下三角掩码失效

问题背景

使用以下26×26 DataFrame(可通过pandas.DataFrame.to_clipboard()直接导入):

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

编写的代码如下,但生成的热力图未呈现预期的下三角形态,环境为seaborn-0.13.2、Python 3.11.1:

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)

失效原因

seaborn 0.13.x版本中,clustermap默认会对行列进行聚类排序,而原代码使用原始矩阵的形状生成掩码,没有匹配聚类后重新排序的行列顺序,导致掩码与热力图的实际元素位置不对应,因此无法正确遮掉下三角。

修正方案

方案1:保留聚类,匹配排序后的掩码

如果需要保留聚类功能,需从clustermap对象中获取排序后的行列索引,基于此生成对应掩码:

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

# 假设matrix已导入
matrix = matrix.astype(int)

# 生成clustermap
cg = sns.clustermap(matrix, annot=True, cmap="Blues", cbar_pos=(.09, .6, .05, .2))
cg.ax_row_dendrogram.set_visible(False)
cg.ax_col_dendrogram.set_visible(False)

# 获取聚类后的行列排序索引
row_order = cg.dendrogram_row.reordered_ind
col_order = cg.dendrogram_col.reordered_ind

# 生成匹配排序后的下三角掩码
mask = np.tril(np.ones_like(matrix, dtype=bool))
mask = mask[row_order][:, col_order]

# 应用掩码到热力图
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.savefig("dqa_eplet_distances_abv.png", dpi=600)
plt.close()

方案2:关闭聚类,直接使用原始掩码

如果不需要聚类(已关闭树状图),可以在生成clustermap时禁用聚类,这样行列保持原始顺序,掩码可直接使用:

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

matrix = matrix.astype(int)

# 关闭行列聚类,保持原始顺序
cg = sns.clustermap(matrix, annot=True, cmap="Blues", cbar_pos=(.09, .6, .05, .2),
                    row_cluster=False, col_cluster=False)
cg.ax_row_dendrogram.set_visible(False)
cg.ax_col_dendrogram.set_visible(False)

# 生成原始下三角掩码
mask = np.tril(np.ones_like(matrix, dtype=bool))

# 应用掩码
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.savefig("dqa_eplet_distances_abv.png", dpi=600)
plt.close()

内容的提问来源于stack exchange,提问作者FluidMechanics Potential Flows

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 00:37:01