I have a dataset with x and y variables. I plotted them in a regular plot:
Now I want to plot the contour level of the density probability contour level eg: 50% of the values are in this area. I tried with seaborn with the following code:
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# define my x an y axes
np.random.seed(0)
x = np.random.randn(100)
y = np.random.randn(100)
# Create a joint plot with scatter plot and KDE contour lines
sns.jointplot(x = x, y = y, kind = "scatter", color = 'b')
sns.kdeplot(x = x, y = y, color = "r", levels = 5)
plt.ylim(0, 17.5)
plt.xlim(0, 20)
# Show the plot
plt.show()
But I would like to choose the contour level values. I searched a long time for a solution but didn't find any really… Is there a simple way of doing this ?

Not sure that contour labelling is directly accessible in
seaborn.matplotlibhas aclabelfunction that adds labels to contours. I've wrapped it in a functionoverlay_labelled_contours()that overlays contours onto your existing scatter plot's axis.The data + code below shows how you can get labelled contours at different quantiles (similar to seaborn's
kdeplot(levels=...).Imports and data:
Overlay contours:
Function that handles the contours:
How it works
The x and y data coordinates are first organised into a two-column matrix shaped
n_samples x 2:A KDE model is fitted on that data (it needs the data as
2 x n_samples, so we supply the transpose of the data matrix):After fitting the KDE, we get the density estimates at the data points:
Since you are interested in quantiles, we map the estimated densities to data proportions. In other words, we learn a mapping that takes in a density values, and tells us what proportion of the data lies below that value. This allows us to convert density data to quantile data.
We will be drawing the contours in a 2D space, so we define a 2D grid of coordinates:
For each point on the grid, use the fitted KDE model to estimate the density at that point:
We then map those estimated densities over the entire 2D area to proportions of the data, since we want the contours to represent proportions of the data.
Finally,
ax.contouris used to display and label contours at the user-defined quantile levels.A fully-
matplotlibapproach, including histograms at the margins.The data is plotted on a
plt.scatterplot, with the labelled contours overlaid usingoverlay_labelled_contours(). Finally, histograms are added at the margins usingmake_axes_locatable().