Increase Heatmap Font Size in Seaborn

Salman Mehmood May 21, 2022
Increase Heatmap Font Size in Seaborn

We start this article with the basics of the heatmap. We will learn what a heatmap is and how to annotate our heatmap.

We will also look at how to change our tick labels font size in a Seaborn heatmap.

Increase Heatmap Font Size in Seaborn

The heatmap is a data visualization tool used to represent graphically the magnitude of data using colors. It helps identify values easily from a given set of data.

We will start by importing the Seaborn library, Matplotlib, and NumPy. We will load some data from Seaborn, which is about cars.

import seaborn as sb
import matplotlib.pyplot as plot
import numpy as np

CARS = sb.load_dataset('mpg')
CARS.head()

Each row gives the statistics about a specific car.

seaborn heatmap font size output 1

Let’s go ahead and group up some of that data before building our heat map.

CARS.groupby('origin').cylinders.value_counts()

We will group by origin or region where each car was made and then also look at the number of cylinders of each car. We are just doing a value count, so we know that there were 63 different cars with four cylinders from Europe, etc.

seaborn heatmap font size output 2

We can see that these data have this multi-level index, so we need to unstack our data. We will fill those missing values with zeros.

ORIGIN_CYL=CARS.groupby('origin').cylinders.value_counts().unstack().fillna(0)

Now we know how many cars were produced in each region with each number of cylinders.

seaborn heatmap font size output 3

Now we are ready to build our first heatmap. To build a heatmap within Seaborn, we only need to reference the Seaborn library by calling up the heatmap() method and passing the ORIGIN_CYL data frame.

import seaborn as sb
import matplotlib.pyplot as plot
import numpy as np

CARS = sb.load_dataset('mpg')
CARS.head()

CARS.groupby('origin').cylinders.value_counts()

ORIGIN_CYL=CARS.groupby('origin').cylinders.value_counts().unstack().fillna(0)
sb.heatmap(ORIGIN_CYL)
plot.show()

We can now see the different rows and columns here, and we have mapped each of those values to a specific color. The lower values were mapped to the darker shades, and higher values were mapped to the lighter shades.

seaborn heatmap font size output 4

We can transpose our heatmap pretty easily using the T property.

import seaborn as sb
import matplotlib.pyplot as plot
import numpy as np

CARS = sb.load_dataset('mpg')
CARS.head()

CARS.groupby('origin').cylinders.value_counts()

ORIGIN_CYL=CARS.groupby('origin').cylinders.value_counts().unstack().fillna(0)
sb.heatmap(ORIGIN_CYL.T)
plot.show()

It will completely invert our matrix. Now cylinders represent the rows, and origins represent columns.

seaborn heatmap font size output 5

We can also style the annotations through an argument called annot_kws. This argument accepts the dictionary, and we can pass different types of properties.

We can change the fontsize, fontweight, and fontfamily.

import seaborn as sb
import matplotlib.pyplot as plot
import numpy as np

CARS = sb.load_dataset('mpg')
CARS.head()

CARS.groupby('origin').cylinders.value_counts()

ORIGIN_CYL=CARS.groupby('origin').cylinders.value_counts().unstack().fillna(0)

sb.heatmap(ORIGIN_CYL,
            cmap='Blues',
            annot=True,
            fmt=".0f",
            annot_kws={
                'fontsize': 16,
                'fontweight': 'bold',
                'fontfamily': 'serif'
            }
           )


plot.show()

The fontsize property will increase our heatmap font size.

seaborn heatmap font size output 6

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.

import seaborn as sb
import matplotlib.pyplot as plot
import numpy as np

CARS = sb.load_dataset('mpg')

sb.heatmap(CARS.corr(),cmap='RdBu',square=True)

plot.show()

Output:

seaborn heatmap font size output 7

Salman Mehmood avatar Salman Mehmood avatar

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.

LinkedIn

Related Article - Seaborn Heatmap