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.

Hexbin Plots

DS 2023 | Communicating with Data

hexbin-plot.png

Overview

A hexbin plot is a density chart that, like a scatter plot, displays the relationship between two numeric variables.

However, instead of representing data points as dots, it divides the plot into a grid of hexagonal bins.

Each hexagonal bin is colored based on the number of points it contains.

Use cases

The main purpose of a hexbin plot is to overcome a major limitation of scatter plots: they don’t do a good job of showing how many data points exists at a specific location.

This problem is called occlusion, meaning that some data points occlude others.

This prevents you from seeing how many data points there are in a given region on the map.

Hexbins solve this problem, although they do this at the cost of showing precisely where individual data points reside.

Hence it’s a good idea to complement scatter plots with hexbin plots.

A simple example

In the case of the Penguins data set, you can see that the three clusters have more points in their centroids, but it is not clear how many points are being covered up by other points.

<Figure size 640x480 with 1 Axes>

One way to address the problem is to set the alpha parameter to a number less than 1.

In graphics lingo, the variable alpha α\alpha stands for the opacity of a visual element.

With a low alpha, stacked points appear darker because the colors of overlapping data points add up.

<Figure size 640x480 with 1 Axes>

This is good, but we can do better.

Another approach is to divide the space into cells and color each cell as a function of the number of data points in the cell.

Pandas provides a .plot.hexbin() method that divides the space into hexagons and shades them by the concentration of data points.

<Figure size 640x480 with 2 Axes>

To get the most out of this method, it is best to choose a gridsize value large enough to group a significant number of points in each cell.

Here we use a value of 25.

<Figure size 640x480 with 2 Axes>

Finally, we can choose a color map that accentuates the distribution.

<Figure size 640x480 with 2 Axes>

The resulting image is a kind of heatmap.

We’ll review these in a later chapter.

A more complex example

Let’s look at a data set with more occlusion.

The following data set for abalone — a large sea snail with rich, flavorful meat — is from the UC Irvine Machine Learning Repository.

According to the UCI site, the data set may used to predict the age of an abalone snail from physical measurements.

Conveniently, the data are available through a Python package ucimlrepo.

Loading...

Notice how many numeric features there are.

In principle, we may be interested in any of the possible pairs of these features.

Because there are 8 numeric features, there are 28 pairs to compare, since the number of pairs is given by n2−n2\Large\frac{n^2 - n}{2}.

Pandas provides a useful tool for viewing all of these at once — a scatter matrix pd.plotting.scatter_matrix().

It creates a matrix comparing each feature to the others.

Along the diagonal, it shows a histogram of each feature. (We’ll look at histograms in the next unit.)

We can use a scatter matrix to find a candidate pair of features for further investigation with a hexbin plot.

<Figure size 1000x1000 with 64 Axes>

From this matrix, the Rings and Whole_weight plots looks like it might contain lot of occlusion.

Let’s see.

<Figure size 640x480 with 1 Axes>

Now, here is a hexbin version.

<Figure size 750x500 with 2 Axes>

The hexbin plot gives us a much better representation of the concentration of data.

You would never know from the scatter plot that there is a high concentration of observations between 5 and 10 on the x axis.