Created
April 27, 2016 20:06
-
-
Save duvenaud/ed9467186db9ce67333badde9613e330 to your computer and use it in GitHub Desktop.
Nice transparent densities overlaid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from matplotlib.colors import LinearSegmentedColormap | |
from scipy.stats import norm | |
def plot_transparent_hexbin(ax, x, y, c, color): | |
cdict = {'red': ((0., color[0], color[0]), (1., color[0], color[0])), | |
'green': ((0., color[1], color[1]), (1., color[1], color[1])), | |
'blue': ((0., color[2], color[2]), (1., color[2], color[2])), | |
'alpha': ((0., 0., 0.), (1., 1., 1.))} | |
new_cmap = LinearSegmentedColormap('Custom', cdict) | |
plt.register_cmap(cmap=new_cmap) | |
ax.hexbin(x.ravel(), y.ravel(), c.ravel(), | |
cmap=new_cmap, linewidths=0., edgecolors='none', | |
gridsize=50, vmin=0., vmax=np.max(c)) | |
def eval_on_grid(func, xlims=[0., 1.], ylims=[0., 1.], granularity=100): | |
X, Y = np.meshgrid(np.linspace(xlims[0], xlims[1], granularity), | |
np.linspace(ylims[0], ylims[1], granularity)) | |
xy = np.vstack([X.ravel(), Y.ravel()]) | |
return X, Y, func(xy).reshape(X.shape) | |
colors = ((1., 0., 0.), (0., 0, 0.), (0., 1., 0.), (1., 1., 0.), (1., 1., 0.),) | |
plt.figure(1) | |
ax = plt.subplot() | |
for i in xrange(5): | |
density_func = lambda x: norm.pdf(x[0, :] - i * 0.1, x[1, :], 1.0 / (0.9 * ( i + 10))) | |
X, Y, C = eval_on_grid(density_func) | |
plot_transparent_hexbin(ax, X, Y, C, colors[i]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool code