Hexbin Plots
DS 2023 | Communicating with Data

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.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
penguins = sns.load_dataset('penguins').dropna()
penguins.plot.scatter(x='bill_length_mm', y='bill_depth_mm')
plt.show()
One way to address the problem is to set the alpha parameter to a number less than 1.
In graphics lingo, the variable 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.
penguins.plot.scatter(x='bill_length_mm', y='bill_depth_mm', alpha=.3)
plt.show()
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.
penguins.plot.hexbin(x='bill_length_mm', y='bill_depth_mm')
plt.show()
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.
penguins.plot.hexbin(x='bill_length_mm', y='bill_depth_mm', gridsize=25)
plt.show()
Finally, we can choose a color map that accentuates the distribution.
penguins.plot.hexbin(x='bill_length_mm', y='bill_depth_mm', gridsize=25, cmap="YlGnBu")
plt.show()
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.
from ucimlrepo import fetch_ucirepo
abalone_raw = fetch_ucirepo(id=1)
abalone = abalone_raw.data['original']
abaloneNotice 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 .
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.
pd.plotting.scatter_matrix(abalone, figsize=(10,10))
plt.show()
From this matrix, the Rings and Whole_weight plots looks like it might contain lot of occlusion.
Let’s see.
abalone.plot.scatter(x='Rings', y='Whole_weight', alpha=.25)
plt.show()
Now, here is a hexbin version.
abalone.plot.hexbin(x='Rings', y='Whole_weight', gridsize=25, figsize=(7.5,5))
plt.show()
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.