python - 带有seaborn clustermap的下三角掩码

标签 python matplotlib seaborn

如何在使用seaborn的聚类图进行分层聚类时掩盖下三角形?

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

#pearson coefficients
corr = np.corrcoef(np.random.randn(10, 200))

#lower triangle
mask = np.tril(np.ones_like(corr))
fig, ax = plt.subplots(figsize=(6,6))

#heatmap works as expected
sns.heatmap(corr, cmap="Blues", mask=mask, cbar=False)

#clustermap not so much
sns.clustermap(corr, cmap="Blues", mask=mask, figsize=(6,6))
plt.show()

enter image description here

最佳答案

嗯,clustermap根据相似性对值进行聚类。这会更改行和列的顺序。

您可以创建一个常规聚类图,然后在第二步中应用掩码:

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

corr = np.corrcoef(np.random.randn(10, 200))

g = sns.clustermap(corr, cmap="Blues", figsize=(6, 6))

mask = np.tril(np.ones_like(corr))
values = g.ax_heatmap.collections[0].get_array().reshape(corr.shape)
new_values = np.ma.array(values, mask=mask)
g.ax_heatmap.collections[0].set_array(new_values)

plt.show()

sns.clustermap with lower triangle mask

关于python - 带有seaborn clustermap的下三角掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67879908/

相关文章:

python - AttributeError ("module ' apache_beam.io.gcp.internal.clients.storage'没有属性 'StorageV1'“)

python - 使用 PIL 和 NumPy 求一组图片的中位数,得到黑白图像

python - 如何对聚合数据进行分组和绘制?

python - 将条形图转变为正态分布

python - 所有标记的 seaborn 散点图标记大小

python - NumPy:将小数转换为分数

python - 使用 cx_oracle 从 MERGE 返回受影响的行数

Python - 如何将 'boundary edge' 绘制到 2D 绘图上

python - 使用 Python 在 Power BI 中筛选 Python Script Visual 中的数据

python - 使用旋转的 xlabels 在 Seaborn Python 中绘制条形图