Exemple de comment ajouter une bordure dans une figure réalisée avec heatmap de seaborn en python
Tracer une figure réalisée avec heatmap
Exemple de comment tracer une heatmap avec seaborn
import seaborn as snsimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltdata = np.array([[25.55535942, 1.99598017, 9.78107706],[ 4.95758736, 39.68268716, 16.78109873],[ 0.45401194, 0.10003128, 0.6921669 ]])df = pd.DataFrame(data=data)fig = plt.figure(num=None, figsize=(10, 10), dpi=80, facecolor='w', edgecolor='k')cmap = sns.cubehelix_palette(light=1, as_cmap=True)res = sns.heatmap(df, annot=True, vmin=0.0, vmax=100.0,fmt='.2f', cmap=cmap, cbar_kws={"shrink": .82})res.invert_yaxis()plt.title('Seaborn heatmap - with frame')plt.savefig('seaborn_heatmap_with_frame_and_cell_border_01.png')plt.show()

Ajouter une bordure à la figure
Pour ajouter une bordure sur le contour de la figure uniquement , une solution est d'ajouter les lignes suivantes:
for _, spine in res.spines.items():spine.set_visible(True)
ce qui donne
import seaborn as snsimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltdata = np.array([[25.55535942, 1.99598017, 9.78107706],[ 4.95758736, 39.68268716, 16.78109873],[ 0.45401194, 0.10003128, 0.6921669 ]])df = pd.DataFrame(data=data)fig = plt.figure(num=None, figsize=(10, 10), dpi=80, facecolor='w', edgecolor='k')cmap = sns.cubehelix_palette(light=1, as_cmap=True)res = sns.heatmap(df, annot=True, vmin=0.0, vmax=100.0,fmt='.2f', cmap=cmap, cbar_kws={"shrink": .82})res.invert_yaxis()# make frame visiblefor _, spine in res.spines.items():spine.set_visible(True)plt.title('Seaborn heatmap - with frame')plt.savefig('seaborn_heatmap_with_frame_01.png')plt.show()

Ajouter une bordure aux cellules
Autre option est d'ajouter les arguments linewidths=0.1 et linecolor='gray' dans la fonction heatmap:
import seaborn as snsimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltdata = np.array([[25.55535942, 1.99598017, 9.78107706],[ 4.95758736, 39.68268716, 16.78109873],[ 0.45401194, 0.10003128, 0.6921669 ]])df = pd.DataFrame(data=data)fig = plt.figure(num=None, figsize=(10, 10), dpi=80, facecolor='w', edgecolor='k')cmap = sns.cubehelix_palette(light=1, as_cmap=True)res = sns.heatmap(df, annot=True, vmin=0.0, vmax=100.0,fmt='.2f', cmap=cmap, cbar_kws={"shrink": .82},linewidths=0.1, linecolor='gray')res.invert_yaxis()plt.title('Seaborn heatmap - with frame')plt.savefig('seaborn_heatmap_with_frame_and_cell_border_01.png')plt.show()
ce qui donne

Références
| Liens | Site |
|---|---|
| seaborn heatmap with frames | stackoverflow |
| seaborn.heatmap | seaborn doc |
