python saborn.heatmap annotation as symbols

Viewed 322

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

Related