How to change the colorbar size of a seaborn heatmap figure in python ?

Published: April 15, 2020

DMCA.com Protection Status

Example of how to change the colorbar size of a seaborn heatmap figure in python:

Create a seaborn heatmap figure

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

How to change the colorbar size of a seaborn heatmap figure in python ?
How to change the colorbar size of a seaborn heatmap figure in python ?

Change colorbar size

To change tje colorbar size a solution is to use the argument cbar_kws={"shrink": .70} in the function 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', 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()

How to change the colorbar size of a seaborn heatmap figure in python ?
How to change the colorbar size of a seaborn heatmap figure in python ?

References

Image

of