Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict a correlation heatmap to interesting cells and add stars to mark exceptional values?

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.

like image 929
Filippo Avatar asked Nov 28 '25 02:11

Filippo


1 Answers

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()

example heatmap

Here is how it looks like with the empty rows and columns removed. Note that it doesn't look perfectly triangular anymore.

empty rows/columns removed

like image 107
JohanC Avatar answered Nov 30 '25 16:11

JohanC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!