Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change tick size on colorbar of seaborn heatmap

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?

like image 220
pbreach Avatar asked Jan 08 '15 02:01

pbreach


People also ask

How do I increase font size in heatmap?

We can change the fontsize , fontweight , and fontfamily . The fontsize property will increase our heatmap font size. We can resize those rectangles using a square argument. We can specify if we would like each of those rectangles to be a perfect square; we can turn this on by setting it equal to True.

What is FMT Seaborn?

cmap: The mapping from data values to color space. center: The value at which to center the colormap when plotting divergent data. annot: If True, write the data value in each cell. fmt: String formatting code to use when adding annotations. linewidths: Width of the lines that will divide each cell.


2 Answers

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)
like image 124
mwaskom Avatar answered Sep 30 '22 18:09

mwaskom


A slightly different way that avoids gcf():

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

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

fig, ax = plt.subplots()
sns.heatmap(arr, ax=ax)
ax.tick_params(labelsize=20)

I almost always start my plots this way, by explicitly creating a fig and ax object. It's a bit more verbose, but since I tend to forget my matplotlib-foo, I don't get confused with what I'm doing.

like image 28
cd98 Avatar answered Sep 30 '22 18:09

cd98