Heatmap


A heatmap is a graphical representation of data where each value of a matrix is represented as a color. This page explains how to build a heatmap with Python, with an emphasis on the Seaborn library. Note that this online course is another way to learn about heatmaps with python.

⏱ Quick start

If you're in a rush and want to make a heatmap with Python as quick as possible, have a look to this code snippet that uses the heatmap() function of Seaborn.🔥

# library
import seaborn as sns
import pandas as pd
import numpy as np

# Create a dataset
df = pd.DataFrame(np.random.random((5,5)), columns=["a","b","c","d","e"])

# Default heatmap
p1 = sns.heatmap(df)

Seaborn logoHeatmap with Seaborn

Seaborn is a python library allowing to make better charts easily thanks to its heatmap() function. This section starts with a post describing the basic usage of the function based on any kind of data input. Next it will guide you through the different ways to customize the chart, like controling color and data normalization.

⚠️ Python heatmap and normalization

Consider the left heatmap below. The second column from the left (variable 1) has very high values compared to others. As a result, the variation existing in other variables is hidden.

Highlighting the variable 1 can be the main message of your chart. But if you're interested in other variable variations as well, you probably want to apply some normalization as shown on the right heatmap.

If you want to no more about normalization, check data-to-viz.com. If you want some python code to do it, it's here.

❄ Python, Heatmap and Clustering

It is very common to apply some clustering techniques on a heatmap. The idea is to group items that have the same kind of pattern for their numeric variables. 💡

Usually, it is recommended to display a dendrogram on top of the heatmap to explain how the clusterization has been performed. Last but not least, it can be useful to compare the grouping we got with an expected structure, shown as an additional color.

A seaborn heatmap with clusterization and dendrogram applied

Heatmap for timeseries

A heatmap can be used to display some temporal data. Here is an example using matplotlib where the evolution of a temperature is displayed over the hour of the day (Y axis) and the day of the year (X axis) organized by month.

A heatmap for temporal data with Python and Matplotlib

Contact

👋 This document is a work by Yan Holtz. Any feedback is highly encouraged. You can fill an issue on Github, drop me a message onTwitter, or send an email pasting yan.holtz.data with gmail.com.

Violin

Density

Histogram

Boxplot

Ridgeline

Scatterplot

Heatmap

Correlogram

Bubble

Connected Scatter

2D Density

Barplot

Spider / Radar

Wordcloud

Parallel

Lollipop

Circular Barplot

Treemap

Venn Diagram

Donut

Pie Chart

Dendrogram

Circular Packing

Line chart

Area chart

Stacked Area

Streamgraph

Timeseries with python

Timeseries

Map

Choropleth

Hexbin

Cartogram

Connection

Bubble

Chord Diagram

Network

Sankey

Arc Diagram

Edge Bundling

Colors

Interactivity

Animation with python

Animation

Cheat sheets

Caveats

3D