Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn, change font size of the colorbar

I wanted to change the font size for a heatmap colorbar.

The following is my code:

import seaborn as sns
import matplotlib.pyplot as plt
from numpy import arange
x = arange(25).reshape(5, 5)
cmap = sns.diverging_palette(220, 20, sep=20, as_cmap=True)
ax = sns.heatmap(x, cmap=cmap)
plt.show()

I was able to change the tick labels with plt.tick_params(axis='both', labelsize=20). However, the colorbar font size does not change. Is there a way to do that?

enter image description here

like image 268
CentAu Avatar asked May 15 '16 00:05

CentAu


People also ask

How do I change font size in Python Colorbar?

MatPlotLib with Python Create a colorbar with a scalar mappable object image. Initialize a variable for fontsize to change the tick size of the colorbar. Use axis tick_params() method to set the tick size of the colorbar. To display the figure, use show() method.

How do I change the title size in Seaborn plot?

We can change the configurations and theme of a seaborn plot using the seaborn. set() function. To set the font size, we use the font_scale parameter in this function. This parameter automatically alters the font of everything in the graph, from the legend to both the axis labels and everything.


2 Answers

You can use matplotlib.axes.Axes.tick_params with labelsize.

For example, your plot with labelsize 20:

import seaborn as sns
import matplotlib.pyplot as plt
from numpy import arange
x = arange(25).reshape(5, 5)
cmap = sns.diverging_palette(220, 20, sep=20, as_cmap=True)
ax = sns.heatmap(x, cmap=cmap)
# use matplotlib.colorbar.Colorbar object
cbar = ax.collections[0].colorbar
# here set the labelsize by 20
cbar.ax.tick_params(labelsize=20)
plt.show()

enter image description here

I refered to the following answer:
 - Using matplotlib.colorbar.Colorbar object
 - Setting parameter

like image 130
Taewoo Lee Avatar answered Sep 25 '22 05:09

Taewoo Lee


You can change the font scale with the seaborn.set() method setting the font_scale param to the scale you want, see more in seaborn documentation.

For example, your plot with scale 3:

import seaborn as sns
import matplotlib.pyplot as plt
from numpy import arange
# here set the scale by 3
sns.set(font_scale=3)
x = arange(25).reshape(5, 5)
cmap = sns.diverging_palette(220, 20, sep=20, as_cmap=True)
ax = sns.heatmap(x, cmap=cmap)
plt.show()

plot with big font scale

like image 33
Filipe Amaral Avatar answered Sep 24 '22 05:09

Filipe Amaral