Change tick size on colorbar of seaborn heatmap

pbreach picture pbreach · Jan 8, 2015 · Viewed 10.5k times · Source

I want to increase the tick label size corresponding to the colorbar in a heatmap plot created using the seaborn module. As an example:

import seaborn as sns
import pandas as pd
import numpy as np

arr = np.random.random((3,3))
df = pd.DataFrame(arr)
ax = sns.heatmap(arr)

Usually I would change the labelsize keyword using the tick_params method on a colorbar axes object, but with the heatmap() function I can only pass kwargs to the colorbar constructor. How can I modify the tick label size for the colorbar in this plot?

Answer

mwaskom picture mwaskom · Jan 8, 2015

Once you call heatmap the colorbar axes will get a reference at the axes attribute of the figure object. So you could either set up the figure ahead of time or get a reference to it after plotting with plt.gcf and then pull the colorbar axes object out that way:

import seaborn as sns
import pandas as pd
import numpy as np

arr = np.random.random((3,3))
df = pd.DataFrame(arr)
ax = sns.heatmap(arr)

cax = plt.gcf().axes[-1]
cax.tick_params(labelsize=20)