Skip to content

Instantly share code, notes, and snippets.

@mattpitkin
Last active May 18, 2017 15:44
Show Gist options
  • Save mattpitkin/7f270853f1a70143e48b1c6c016ae23b to your computer and use it in GitHub Desktop.
Save mattpitkin/7f270853f1a70143e48b1c6c016ae23b to your computer and use it in GitHub Desktop.
Example for producing a contour plot and also plotting the points outside the outer contour
import numpy as np
from matplotlib import pyplot as pl
x = np.random.randn(5000)
y = 0.2*np.random.randn(5000) + 3
xbins = 20
ybins = 20
H, xedges, yedges = np.histogram2d(x, y, bins=(xbins, ybins))
# set levels as in IDL djs_contourpts https://searchcode.com/codesearch/view/84370/
nlevels = 3
levels = ((np.arange(nlevels)+1) * np.max(H[:])) / float(nlevels)
C = pl.contour(H.T, extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], levels=levels)
p = C.collections[0].get_paths()[0]
poutside = ~p.contains_points(np.vstack((x,y)).T)
pl.plot(x[poutside], y[poutside], '.')
@mattpitkin
Copy link
Author

mattpitkin commented May 18, 2017

This example stems from this facebook post.

Following another comment on that post my example seems to be similar to what is done in the astroML scatter_contour function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment