Auto adjust font size in seaborn heatmap

PythonMatplotlibSeaborn

Python Problem Overview


when using seaborn heatmap, is there a way to auto-adjust the font size for it to fit exactly inside the squares? for example in:

sns.heatmap(corrmat, vmin=corrmat.values.min(), vmax=1, square=True, cmap="YlGnBu", 
        linewidths=0.1, annot=True, annot_kws={"size":8})  

here the size is set in "annot_kws".

Python Solutions


Solution 1 - Python

You can also do:

sns.heatmap(corrmat, vmin=corrmat.values.min(), vmax=1, square=True, cmap="YlGnBu", linewidths=0.1, annot=True, annot_kws={"fontsize":8})  

Solution 2 - Python

If you want something automatic this is not bad:

annot_kws={"size": 35 / np.sqrt(len(corrmat))},

Solution 3 - Python

To adjust the font size of seaborn heatmap, there are different methods

import seaborn as sns # for data visualization
flight = sns.load_dataset('flights') # load flights datset from GitHub seaborn repository

# reshape flights dataeset in proper format to create seaborn heatmap
flights_df = flight.pivot('month', 'year', 'passengers') 

sns.heatmap(flights_df) # create seaborn heatmap

sns.set(font_scale=2) # font size 2

Output >>>

enter image description here

the sns.set(font_scale=2) # font size 2 set size for all seaborn graph labels that's reason follow another method if you like

import seaborn as sns # for data visualization
import matplotlib.pyplot as plt # for data visualization

flight = sns.load_dataset('flights') # load flights datset from GitHub seaborn repository

# reshape flights dataeset in proper format to create seaborn heatmap
flights_df = flight.pivot('month', 'year', 'passengers') 

sns.heatmap(flights_df) # create seaborn heatmap


plt.title('Heatmap of Flighr Dataset', fontsize = 20) # title with fontsize 20
plt.xlabel('Years', fontsize = 15) # x-axis label with fontsize 15
plt.ylabel('Monthes', fontsize = 15) # y-axis label with fontsize 15

plt.show()

Output >>>

enter image description here

Solution 4 - Python

Although it distorts the heatmap, this example illustrates how to scale the font using .set(...) context

import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font_scale=3)

# Load the example flights dataset and conver to long-form
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")

# Draw a heatmap with the numeric values in each cell
f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(flights, annot=True, fmt="d", linewidths=.5, ax=ax)
f.savefig("output.png")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGabrielView Question on Stackoverflow
Solution 1 - Pythondex314View Answer on Stackoverflow
Solution 2 - PythonRuggero TurraView Answer on Stackoverflow
Solution 3 - PythonRudra MohanView Answer on Stackoverflow
Solution 4 - PythonSupreet SethiView Answer on Stackoverflow