Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Heatmaps

DS 2023 | Communicating with Data

Overview

A heatmap displays a matrix of numbers as a grid of colored cells, where the color value in a cell is a function of the number value there.

Heatmaps show the relationship between two grouping variables and a measurement variable by using the visual channel of color to indicate intensity.

Use Cases

Visualizing wide-form data. Heatmaps can almost always be used to show the distribution of values in a wide table. As such, they are a good compliment to subplots generated from such tables.

Seeing structure in a large table at a glance. The advantage of a heatmap is that it shows two-dimensional patterns: blocks, stripes and criss-crosses, and gaps jump out that you would never notice scanning raw numbers.

Correlation and confusion matrices. A square matrix of relationships between variables (or predicted-vs-actual class counts) is often shown as a heatmap.

Contingency matrices. A heatmap can be used to show the pair-wise, co-ocurrence data. They are a good compliment to network diagrams of the same data.

Creating a heatmap from scratch

Let’s create a heatmap from scratch.

We’ll use a dataset of passenger counts on airline flights grouped by year and month.

This gives us a table of two grouping variables and a measurement one.

<class 'pandas.DataFrame'>
RangeIndex: 144 entries, 0 to 143
Data columns (total 3 columns):
 #   Column      Non-Null Count  Dtype
---  ------      --------------  -----
 0   Years       144 non-null    int64
 1   Months      144 non-null    str  
 2   Passengers  144 non-null    int64
dtypes: int64(2), str(1)
memory usage: 4.4 KB
Loading...

To ensure that the months display in the right order, we convert these into Categorical data type.

<ArrowStringArray> [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] Length: 12, dtype: str

First, we create a wide dataframe.

Loading...

Then, we scale all the values to be in [0,1][0,1].

vscaled=v−vmin⁡vmax⁡−vmin⁡,vscaled∈[0,1].v_{scaled} = \frac{v - v_{\min}}{v_{\max} - v_{\min}}, \qquad v_{scaled} \in [0, 1].
Loading...

Next, we map the scaled values to the corresponding value on a color scale.

Here we get a colormap object from Matplotlib called YlGnBu.

Loading...

The colormap as 256 color values, show above as a gradient.

256

We can take any number in [0,1][0, 1] and get the corresponding color code for it.

Here we see the results of converting .5, showing the result as an indexed series for demonstration purposes.

r 0.252687 g 0.711449 b 0.768381 a 1.000000 dtype: float64

Often, we will convert these values to [0,255][0, 255].

r 64 g 181 b 195 a 255 dtype: int64

Or, we convert to hexadecimal, the format that color codes are often expressed.

r 0x40 g 0xb5 b 0xc3 a 0xff dtype: str

Now, we convert the dataframe to RGBA values.

Loading...

Convert the RGB tuples into HTML entities.

Loading...

Using Matplotlib

Matplotlib’s plt.imshow() will take a 2D array and color each entry according to the specified colormap, defaulting to viridis.

<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>

Using Seaborn

<Figure size 640x480 with 2 Axes>
<Figure size 640x480 with 2 Axes>
<Figure size 640x480 with 2 Axes>
<Figure size 1000x1000 with 2 Axes>

Using Pandas

Because a heatmap is basically a colored-in wide-form table, Pandas let’s you style a dataframe as a heatmap with .style.background_gradient.

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Using Plotly

Loading...