First, you need a density estimation of you data. Depending on the method you choose, varying result can be obtained.
Let's assume you want to do gaussian density estimation, based on the example of scipy.stats.gaussian_kde, you can get the density height with:
def density_estimation(m1, m2):
X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([m1, m2])
kernel = stats.gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape)
return X, Y, Z
Then, you can plot it with contour with
X, Y, Z = density_estimation(m1, m2)
fig, ax = plt.subplots()
# Show density
ax.imshow(np.rot90(Z), cmap=plt.cm.gist_earth_r,
extent=[xmin, xmax, ymin, ymax])
# Add contour lines
plt.contour(X, Y, Z)
ax.plot(m1, m2, 'k.', markersize=2)
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])
plt.show()
As an alternative, you could change the marker color based on their density as shown here.