如何将相关热图限制在有趣的单元格中,并添加星号以标记特殊值? [英] How to restrict a correlation heatmap to interesting cells and add stars to mark exceptional values?
问题描述
我正在尝试在python中创建一个很好的相关矩阵热图,但是找不到我想要的方式自定义它的选项.
I'm trying to do a nice correlation matrix heatmap in python, but I can't find the options to customize it the way I want.
我的代码就是这个代码:
My code is simply this one:
plt.figure(figsize=(16, 6))
mask = np.triu(np.ones_like(Correlazioni.corr(), dtype=np.bool))
heatmap = sns.heatmap(Correlazioni.corr(), mask=mask, vmin=-1, vmax=1, annot=True, cmap='BrBG')
heatmap.set_title('Triangle Correlation Heatmap', fontdict={'fontsize':18}, pad=16);
现在我想在重要的单元格中添加(*):(例如:当系数高于或低于某个特定值时)
Now I would like to add (*) in significant cells: ( example: when the coefficient is higher or lower of a certain value)
非常感谢您的答复,如果我错过了要求,请告诉我,我会提供的.
Thank you very much for the answers, if I missed anything from my request, please let me know and I will provide it.
推荐答案
要显示较少的单元格,可以扩展掩码,以掩盖不需要的值.不仅可以设置 annot = True
,还可以提供字符串列表.您可以完全控制格式化这些字符串的方式,例如附加星号:
To show less cells, you can extend the mask, masking away the non-wanted values.
Instead of just setting annot=True
, also a list of strings can be provided. You fully control how you format these strings, and e.g. append stars:
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
np.random.seed(124)
Correlazioni = pd.DataFrame(np.random.rand(7, 10), columns=[*'abcdefghij'])
plt.figure(figsize=(16, 6))
corr = Correlazioni.corr()
mask = np.triu(np.ones_like(corr, dtype=np.bool))
cut_off = 0.6 # only show cells with abs(correlation) at least this value
extreme_1 = 0.75 # show with a star
extreme_2 = 0.85 # show with a second star
extreme_3 = 0.90 # show with a third star
mask |= np.abs(corr) < cut_off
corr = corr[~mask] # fill in NaN in the non-desired cells
remove_empty_rows_and_cols = True
if remove_empty_rows_and_cols:
wanted_cols = np.flatnonzero(np.count_nonzero(~mask, axis=1))
wanted_rows = np.flatnonzero(np.count_nonzero(~mask, axis=0))
corr = corr.iloc[wanted_cols, wanted_rows]
annot = [[f"{val:.4f}"
+ ('' if abs(val) < extreme_1 else '\n★') # add one star if abs(val) >= extreme_1
+ ('' if abs(val) < extreme_2 else '★') # add an extra star if abs(val) >= extreme_2
+ ('' if abs(val) < extreme_3 else '★') # add yet an extra star if abs(val) >= extreme_3
for val in row] for row in corr.to_numpy()]
heatmap = sns.heatmap(corr, vmin=-1, vmax=1, annot=annot, fmt='', cmap='BrBG')
heatmap.set_title('Triangle Correlation Heatmap', fontdict={'fontsize': 18}, pad=16)
plt.show()
这是删除空白行和空白列后的样子.请注意,它看起来不再是完美的三角形.
Here is how it looks like with the empty rows and columns removed. Note that it doesn't look perfectly triangular anymore.
这篇关于如何将相关热图限制在有趣的单元格中,并添加星号以标记特殊值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!