How to increase the size of axes labels on a seaborn heatmap in python ?

How to increase the size of axes labels on a seaborn heatmap in python ?

Daidalos May 09, 2020


Examples of how to increase the size of axes labels on a seaborn heatmap in python:

1 -- Create a simple heatmap using seaborn

Let's create a first 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,columns=['C1','C2','C3'], index=['A','B','C'])

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


#res.set_xticklabels(res.get_xmajorticklabels(), fontsize = 18)
#res.set_yticklabels(res.get_ymajorticklabels(), fontsize = 18)

plt.title('Seaborn heatmap - change font size')

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

Annotations can be added using the option:

annot=True

and with the option

fmt='.2f'

to control the format (here two digits after the coma)

How to increase the size of axes labels on a seaborn heatmap in python?

2 -- Increase the size of the labels on the x-axis

To Increase the size of the labels on the x-axis, a solution is to add the line:

res.set_xticklabels(res.get_xmajorticklabels(), fontsize = 18)

Note: to change label rotation:

res.set_xticklabels(res.get_xmajorticklabels(), fontsize = 18, rotation=45)

code complet:

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,columns=['C1','C2','C3'], index=['A','B','C'])

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

res.set_xticklabels(res.get_xmajorticklabels(), fontsize = 18)

plt.title('Seaborn heatmap - change font size')

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

How to increase the size of axes labels on a seaborn heatmap in python?

3 -- Increase the size of the labels on the y-axis

To increase the size of the labels on the y-axis just add the following line:

res.set_yticklabels(res.get_ymajorticklabels(), fontsize = 18)

Note: to control the labels rotation there is the option "rotation":

res.set_yticklabels(res.get_ymajorticklabels(), fontsize = 18, rotation=45)

source code exaple:

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,columns=['C1','C2','C3'], index=['A','B','C'])

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

res.set_yticklabels(res.get_ymajorticklabels(), fontsize = 18)

plt.title('Seaborn heatmap - change font size')

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

How to increase the size of axes labels on a seaborn heatmap in python?

4 -- Increase the size of all the labels in the same time

Note: to increase the size of all the labels in the same time, a simple solution is to use:

sns.set(font_scale=1.4)

example

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,columns=['C1','C2','C3'], index=['A','B','C'])

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

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

How to increase the size of axes labels on a seaborn heatmap in python ?

5 -- References