How to create heatmaps for time series with Python?

Yanet Hernandez Mada
MCD-UNISON
Published in
3 min readNov 25, 2022

The current post will briefly explain how we can include a heatmap for time series analysis using Python.

Heatmaps for time series represent a clear way to visualize our data, presented in a tabular structure, and using colored cells, that would support our analysis. At the same time, using a heatmap we will display over a calendar view, including year, weeks, days, and the numeric value that will be added to the visualization, this will help us identify daily and weekly patterns, and also recognize anomalies within the data.

While we review the rows and colors of the chart, we can quickly identify where higher and lower points are located. The use of color saturation is an easy way to process and identify the points of interest in our data. This makes the heatmap a good way to direct our attention to cues that might not get noticed by just looking at the numbers.

The Calplot library, which is based on Matplotlib, will help us to present heatmaps while using matplotlib customizations.

Data Preparation.

For this tutorial, we are using Jupiter Notebook, so first, we will import pandas to read the dataset.

import pandas as pd

This dataset is available on GitHub and can be downloaded at this link. The information contained on file is for illustrative purposes, focusing on weather conditions, specifically on the actual maximum temperature of the city of Hermosillo, Mexico, from 2020 and 2021.

After importing libraries, we load the dataset.

df_tem = pd.read_csv('max_hmo.csv', delimiter=",")
df_tem.tail(2)

df_tem.tail(2), returns the last 2 rows of the dataset. Printing this information helps us to review the format of the Dataframe we just imported.

Additionally, we need to review the format of our data columns with the following line:

df_tem.info()

If the date column is formatted as an object or string, we need to adjust it to the DateTime format.

For this, we will be following pandas documentation to reformat our column to DateTime.

df_tem['Date'] =  pd.to_datetime(df_tem['Date'], format='%d/%m/%Y')

Please note that we pass the column format, in this case using %d/%m/%Y day, month, and year, accordingly.

Now we verify the column is assigned as DateTime64.

df_tem.info()

Next step is to pass the datetime column as index with the following code:

df_tem = df_tem.set_index('Date')

Creating the chart.

First, we need to install the library with pip install. In the terminal we type

pip install calplot

In python, we import the library with the following line.

import calplot

To create the heatmap, we will pass the name of the Dataframe along with the column that we need to chart.

calplot.calplot(df_tem['Max_temperatures'], cmap="coolwarm", colorbar=False);

Customize heatmap.

We can add a color bar by setting the value to true, and use the different pattern colors available for matplotlib. Here we are using blue-green “BuGn”.

calplot.calplot(df_tem['Max_temperatures'], cmap="BuGn", colorbar=True);

We can also modify the size of the figure, add titles, line or fill colors.

calplot.calplot(df_tem['Max_temperatures'], 
cmap= cmap1, colorbar=True,
linecolor ="white" , fillcolor= "gray",
figsize = (8, 4),
suptitle = "Temperature by Month and Year");

The plot above is clear yet insightful, is easy to locate the maximum temperatures.

In conclusion, by adding heatmaps to our analysis we can extract patterns and insights from our data.

For the complete customizable options please visit the official calplot documentation.

--

--