Plot contours for the densest region of a scatter plot
4 years later and I can finally answer this! this can be done using contains_points from matplotlib.path.
I've used a Gaussian smoothing from astropy which can be omitted or substituted as needed.
import matplotlib.colors as colors
from matplotlib import path
import numpy as np
from matplotlib import pyplot as plt
try:
from astropy.convolution import Gaussian2DKernel, convolve
astro_smooth = True
except ImportError as IE:
astro_smooth = False
np.random.seed(123)
t = np.linspace(-1,1.2,2000)
x = (t**2)+(0.3*np.random.randn(2000))
y = (t**5)+(0.5*np.random.randn(2000))
H, xedges, yedges = np.histogram2d(x,y, bins=(50,40))
xmesh, ymesh = np.meshgrid(xedges[:-1], yedges[:-1])
# Smooth the contours (if astropy is installed)
if astro_smooth:
kernel = Gaussian2DKernel(stddev=1.)
H=convolve(H,kernel)
fig,ax = plt.subplots(1, figsize=(7,6))
clevels = ax.contour(xmesh,ymesh,H.T,lw=.9,cmap='winter')#,zorder=90)
# Identify points within contours
p = clevels.collections[0].get_paths()
inside = np.full_like(x,False,dtype=bool)
for level in p:
inside |= level.contains_points(zip(*(x,y)))
ax.plot(x[~inside],y[~inside],'kx')
plt.show(block=False)