How to annot only values greater than x on a seaborn heatmap
Asked Answered
I

2

5

I wanted to annot only values greater than 0.4 on my seaborn heatmap.

Here is my code:

sns.set(font_scale=0.6)

sns.set(font_scale=0.6)
ax= sns.heatmap(corr, mask=mask, cmap=cmap, vmin=-1, vmax=+1, center=0,
            square=True, linewidths=.1, cbar_kws={"shrink": .82},annot=True,
            fmt='.1',annot_kws={"size":7})

ax.set_xticklabels(ax.get_xticklabels(), rotation=60)

This is what I get: enter image description here

Thank you

Incontrovertible answered 8/2, 2021 at 10:2 Comment(0)
I
7

Issue solved with a simple loop that iterates over the quadrants and set up the annotations only when the value is greater than 0.4:

for t in ax.texts:
    if float(t.get_text())>=0.4:
        t.set_text(t.get_text()) #if the value is greater than 0.4 then I set the text 
    else:
        t.set_text("") # if not it sets an empty text

enter image description here

Incontrovertible answered 8/2, 2021 at 10:6 Comment(0)
P
0

From version 0.7.1., seaborn added an option of labels array of the same shape as the data (here is the documentation):

# Generate some array to plot
arr = np.arange(16).reshape(4, -1)

# Generate annotation labels array (of the same size as the heatmap data)- filling cells you don't want to annotate with an empty string ''
annot_labels = np.empty_like(arr, dtype=str)
annot_mask = arr > 8
annot_labels[annot_mask] = 'T'  

# Plot hearmap with the annotations
ax= sns.heatmap(arr, annot=annot_labels, fmt='')

enter image description here

Paradies answered 5/8, 2021 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.