How to increase the size of the cells text (annotations) of a seaborn heatmap in python ?

Published: May 09, 2020

Tags: Python; Seaborn; Heatmap;

DMCA.com Protection Status

Example of how to increase the size of the cells text (annotations) of a seaborn heatmap in python:

1 -- Create a simple heatmap using seaborn

Let's first create a simple heatmap using seaborn:

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

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

plt.title('Seaborn heatmap - increase annotations font size')

plt.savefig('seaborn_heatmap_increase_annotations_fontsize_01.png')              
plt.show()

It is then possible to add annotations using:

annot=True

and

fmt='.2f'

to control the format (here keep only 2 digits after the coma)

 How to increase the size of the cells text (annotations) of a seaborn heatmap in python ?
How to increase the size of the cells text (annotations) of a seaborn heatmap in python ?

2 -- Increase cell annotations size (option 1)

To change heatmap cell annotations size, a solution is to use the option:

annot_kws={"size": 18},

in the seaborn function heatmap(), example (see line 18):

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

cmap = sns.cubehelix_palette(light=1, as_cmap=True)

res = sns.heatmap(df, annot=True, vmin=0.0, vmax=100.0, 
                  annot_kws={"size": 18},
                  fmt='.2f', cmap=cmap, cbar_kws={"shrink": .82},
                  linewidths=0.1, linecolor='gray')

plt.title('Seaborn heatmap - increase annotations font size')

plt.savefig('seaborn_heatmap_increase_annotations_fontsize_01.png')              
plt.show()

 How to increase the size of the cells text (annotations) of a seaborn heatmap in python ?
How to increase the size of the cells text (annotations) of a seaborn heatmap in python ?

3 -- Increase cell annotations size (option 2)

Another option using the command:

sns.set(font_scale=1.4)

to change all the heatmap labels size (title; annotations, xlabels and ylabels):

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

cmap = sns.cubehelix_palette(light=1, as_cmap=True)

sns.set(font_scale=1.4)

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 - increase annotations font size')

plt.savefig('seaborn_heatmap_increase_annotations_fontsize_02.png')              
plt.show()

 How to increase the size of the cells text (annotations) of a seaborn heatmap in python ?
How to increase the size of the cells text (annotations) of a seaborn heatmap in python ?

4 -- References

Image

of