Create a Python Heatmap with Seaborn

You can easily create a heatmap using the Seaborn library in Python.  For this tutorial, I’m going to create this using Jupyter Notebooks. The first step is to load the dependencies which are the essential library. You can also Learn Python Data Insights on YouTube

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

Now that we have the essential libraries, lets load in your data set and save it as a variable called df. My data is saved as a CSV. I am going to use Avocado prices I download from Kaggle’s data library.

df = pd.read_csv('avocado.csv')

How does Seaborn Heatmap work?

This heat map works by correlation. This shows you which variables are correlated to each other from a scale of 1 being the most correlated and -1 is not correlated at all. However, you cannot correlate strings. You can only correlate numerical features. Don’t let this stop you because you can always encode your categorical data types using the get_dummies function in Pandas library. I am adding the figure size so that we get a bigger image. You can do this by adding plt.figure() function.

plt.figure(figsize=(10,5)
sns.heatmap(df.corr())

Use a Seaborn to create a correlation heatmap

Once you have the heat map created, let’s make it more actionable by changing the styles. Add correlation numbers to get a better understanding of it. You can do this by adding the annot parameter which will add correlation numbers to each cell in the visuals.

sns.heatmap(df.corr(),annot=True)

Use annotation to add correlation numbers to the Seaborn heatmap

We can style this further by changing the figure size and colors. To do this you will need to use Matplotlib figure function.

plt.figure(figsize=(9,5)
sns.heatmap(df.corr(),annot=True)

Add Lines between the correlation cells

plt.figure(figsize=(9,5)
sns.heatmap(df.corr(),annot=True, linewidth=0.5)

Add lines to your seaborn heatmapChange the Heatmap Colors

Lastly, you can alter the colors of your heatmap by utilizing the cmap parameter. You can Google the Seaborn color palette to see what is available. I’m going to change this to the coolwarm palette. Here is a great resource for colors

plt.figure(figsize=(9,5)
sns.heatmap(df.corr(),annot=True,linewidth = 0.5, cmap='coolwarm')

change the color palette in the seaborn heatmap

Check out the Video on Seaborn Heatmap Creation in Python

Learn how to create Subplots.

Gaelim Holland

Subscribe
Notify of
guest

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Someone
Someone
4 years ago

It’s great to see some essential functions in such an easy-to-follow way – thanks!

BUT: it would be even more great, if the code parts (which are copyable) were correct (no wrong function names, missing brackets and typos) 😉

Mayank Agarwal
Mayank Agarwal
4 years ago

nice tutorial
some bracket error

plt.figure(figsize=(9,5)
should be

plt.figure(figsize=(9,5))

Someone else
Someone else
4 years ago

Hi there!
I’ve got some issue if I make few pictures in row, the each next picture shows the old numbers from previous pictures.
Do i something wrong?
May you guide me please?
Have nice day!

Himanshu Goyal
Himanshu Goyal
3 years ago

Nice Explanation