Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotation as symbols on a seaborn heatmap

I want heatmap annotation as symbols. '*' at place of 1 and blank at 0.

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

x = pd.DataFrame({'a':[1,0,1,0]})
fig, (ax) = plt.subplots(ncols=1)
sns.heatmap(x, cmap="BuPu",annot=True,fmt='g',annot_kws={'size':10},ax=ax, yticklabels=[], cbar=False, linewidths=.5,robust=True, vmin=0, vmax=1)
plt.show()
like image 374
himnahsu Avatar asked Sep 17 '25 05:09

himnahsu


1 Answers

The heatmap can only annotate with numbers. To put other text (or unicode symbols), ax.text can be used. The center of each cell is at 0.5 added to both the row and the column number.

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

x = pd.DataFrame({'a': [1, 0, 1, 0], 'b': [1, 1, 0, 1], 'c': [0, 1, 0, 0]})
fig, (ax) = plt.subplots(ncols=1)
sns.heatmap(x, cmap="BuPu", annot=False, ax=ax, yticklabels=[], cbar=False, linewidths=.5)

for i, c in enumerate(x.columns):
    for j, v in enumerate(x[c]):
        if v == 1:
            ax.text(i + 0.5, j + 0.5, '★', color='gold', size=20, ha='center', va='center')
plt.show()

example plot

like image 106
JohanC Avatar answered Sep 19 '25 19:09

JohanC