Pretty Joint Plots
In this tutorial, I’ll be using Python to create a neat, customizable joint plot–– inspired by the
jointplot graphics found in
Seaborn. Joint plots are great for plotting bivariate datasets, as they’re readily legible and provide high information content. Here, I’ll be creating a KDE joint plot with a modern color palette but a.simple design that resembles an old-school topographical map.
To follow along, you’ll need a working knowledge of statistics as well as
SciPy in order to generate a dataset;
matplotlib to create the figure itself; and
Seaborn to enhance matplotlib’s somewhat bland aesthetics. Note: I’m also using
mpld3 to generate an interactive plot, but this is completely optional.
Assuming you’re familiar with
pip, we can further simplify installation by installing
conda (Although I highly recommend using Anaconda’s python distribution, which comes with
conda is installed, getting
matplotlib is a easy as:
conda will automatically install the packages and their requirements using pre-compiled binaries, so installation times should be on the order of seconds instead of tens of minutes.
Seaborn is only available through pip, however:
Once these packages have been installed, you can get started!
If you don’t already have some data lying around to plot, you can easily create some using a bivariate normal distribution with
NumPy. First, we’ll need an easy way to generate pseudo-random positive-semidefinite matrices for our distribution’s covariances:
Then, we can define a pair of means and covariance matrices for our distribution:
Here, we’ve generated a pair of 2-D means from , where is a random integer pulled from , and and are both random float arrays pulled from . If this all seems cryptic–– don’t worry! These parameters are completely arbitrary, and the point is just that we’re generating an interesting distribution to plot.
To sample 500 points from our bivariate distribution, we can use
So, now we have some data to plot!
Before we go on to making a KDE of our data, let’s just make a joint plot with a scatter plot in the main grid and marginal histograms. First let’s import
Seaborn and set our plot style to
We can then set the x- and y-axes limits:
Although, for aesthetic purposes, we should probably adjust these values so that the all the points are clearly visible. I find that scaling up each axis by a factor of works fairly well:
These values will be useful for making the KDE joint plot as well.
Now, we can use the following code to generate the figure:
Kernel Density Estimation
There are many ways to create a kernel density estimator in Python, but in this tutorial we’ll be using
To extract densities from the
gaussian_kde objects, we need to create some meshgrids using our max- and min-values from before:
Now that we have KDEs for both the marginals and the bivariate distribution, we can make our final plot with the following code:
And there you have it! If you have any questions, please feel free to leave a comment below.