How to Make Heatmaps with Seaborn in Python?

Heatmap with Seaborn using coolwarm colormap
Heatmap with Seaborn using coolwarm colormap

Heatmaps are a way of visualizing the raw data as it is. Heatmaps visualzises a matrix/table/dataframe of numbers as colors. We can easily make a heatmap of a dataframe of numbers using Seaborn.

In this tutorial, we will learn how to make a heatmap using Seaborn’s heatmap function. When you are working to making a data visualization like heatmaps here, the data you have is never in the same format as the data needed for making data visualization. In this tutorial (and website), we will see step-by-step examples of massaging the data needed for making the visualization.

Let us load the packages we need to make a heatmap. We will use a real world dataset from vega_datasets to make a heatmap with Seaborn in Python.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from vega_datasets import data

Heatmap with Seattle Temperature Data using Seaborn

Let us use Seattle temperature data for the year 2010 from vega_datasets package to make heatmap. The Seattle dataset contains date and temperature for every hour for the year 2010.

df = data.seattle_temps()
df.head()

date	temp
0	2010-01-01 00:00:00	39.4
1	2010-01-01 01:00:00	39.2
2	2010-01-01 02:00:00	39.0
3	2010-01-01 03:00:00	38.9
4	2010-01-01 04:00:00	38.8

We will make a heatmap of average temperature of every hour for each month. Let us create month, day, and hour variables from our date stamp.

df['month'] = df['date'].dt.month_name()
df['day'] = df['date'].dt.day_name()
df['hour']=df['date'].dt.hour
df['date']=df['date'].dt.date
df.head()

	date	temp	month	day	hour
0	2010-01-01	39.4	January	Friday	0
1	2010-01-01	39.2	January	Friday	1
2	2010-01-01	39.0	January	Friday	2
3	2010-01-01	38.9	January	Friday	3
4	2010-01-01	38.8	January	Friday	4

Now, our data contains month and day as strings and hours as numbers. Let us compute average temperature for every month and hour. To do that we first groupby() month and year and compute mean temperature.

df1 = df.groupby(['month', 'hour'],sort=False).agg(['mean'])
df1.columns=df1.columns.droplevel(0)
df1.reset_index(inplace=True)
df1.head()


        month	hour	mean
0	January	0	40.712903
1	January	1	40.396774
2	January	2	40.190323
3	January	3	39.990323
4	January	4	39.735484

Reshaping Data from Long to wide form with pivot_table

Now we have the data we need for making a heatmap. We can use Seaborn’s heatmap function to make the heatmap. However, Seaborn’s heatmap function expects the data to be in wide form; months on rows and hours on columns. Our data is in tidy long form.

We can reshape the data in long tidy form to wide form using Pandas’s pivot_table() function. To reshape the data with pivot_table, we need to specify which variable should go to column using columns argument, which variable should be index using index argument and which variable will be the values in the wide table using values argument.

# pandas pivot with multiple variables
heatmap_data = pd.pivot_table(df1, values='mean', index=['month'], columns='hour')
# print to see few rows and columns
print(heatmap_data.iloc[0:3, 0:3])

hour              0          1          2
month                                    
January   40.712903  40.396774  40.190323
February  41.467857  41.032143  40.689286
March     43.648387  43.209677  42.722581

We have the data in wide form now. If you look at the wide form data, you will notice that the months are in alphabetical order, not the order we want.

Let us re-order the dataframe by rows, using row index, Pandas’ loc function and the index in right order as list.

To do that, let us get months in right order from January to December.

months=df1.month.unique().tolist()
print(months)
['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

And use the months in right order to re-order our heatmap data by rows using loc().

heatmap_data=heatmap_data.loc[months]

How To Make Heatmaps in Python?

Finally, we have the data ready to make heatmap with Seaborn’s heatmap() function. Calling the Seaborn’s heatmap() function with the data in wide form is enough to make the simple heatmap. Here we customize the heatmap a bit with x and y-axis labels and title.

sns.heatmap(heatmap_data)
plt.xlabel("Hour", size=14)
plt.ylabel("Month", size=14)
plt.title(" Seattle 2010 Mean Temperature", size=14)
plt.tight_layout()
plt.savefig('heatmap_with_Seaborn_python.jpg',dpi=150, figsize=(9,6))

We get a heatmap colored automatically by Seaborn. Seaborn also plots the legend with color scale and range of values on the right.

Heatmap with Seaborn

How to Change the Colors in Heatmap with Seaborn?

We can change the default colors on the heatmap easily. Seaborn offers a number of ways to color the heatmap automatically using their color palette. In this example, we specify the color palette using cmap=”coolwarm” argument inside heatmap() function.

sns.heatmap(heatmap_data,cmap="coolwarm")
plt.xlabel("Hour", size=14)
plt.ylabel("Month", size=14)
plt.title(" Seattle 2010 Mean Temperature", size=14)
plt.tight_layout()
plt.savefig('heatmap_with_Seaborn_python_coolwarm.jpg',dpi=150, figsize=(9,6))

With a quick look at the heatmap, we can easily tell which months/hours it is warmer and colders

Heatmap with Seaborn using coolwarm colormap

There are a number of other color maps available in Seaborn. Here is the heatmap colored by cmap=”RdBu_r”.

sns.heatmap(heatmap_data,cmap="RdBu_r")
plt.xlabel("Hour", size=14)
plt.ylabel("Month", size=14)
plt.title(" Seattle 2010 Mean Temperature", size=14)
plt.tight_layout()
plt.savefig('heatmap_with_Seaborn_python_RdBu_r.jpg',dpi=150, figsize=(9,6))

Heatmap with Seaborn using RdBu_r
Exit mobile version