Exemple de comment changer la taille de la colorbar dans une figure réalisée avec heatmap de seaborn en python
Table des matières
Tracer une figure avec heatmap
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = 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')
res = sns.heatmap(df, annot=True, vmin=0.0, vmax=100.0,
fmt='.2f', cmap='magma',
linewidths=0.1, linecolor='gray')
res.invert_yaxis()
plt.title('Seaborn - change colormap size')
plt.savefig('seaborn_change_colormap_size_01.png')
plt.show()
Changer la taille de la colorbar
Pour changer la taille de la colorbar, une solution est d'utiliser l'argument cbar_kws={"shrink": .70} dans la fonction heatmap. Exemple:
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = 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')
res = sns.heatmap(df, annot=True, vmin=0.0, vmax=100.0,
fmt='.2f', cmap='magma', cbar_kws={"shrink": .70},
linewidths=0.1, linecolor='gray')
res.invert_yaxis()
plt.title('Seaborn - change colormap size')
plt.savefig('seaborn_change_colormap_size_02.png')
plt.show()
Références
Liens | Site |
---|---|
Change the height of a Seaborn heatmap colorbar | stackoverflow |
seaborn heatmap with frames | stackoverflow |
seaborn.heatmap | seaborn doc |