xyz = np.vstack([x, y, z])
kde = stats.gaussian_kde(xyz)
xmin, ymin, zmin = x.min(), y.min(), z.min()
xmax, ymax, zmax = x.max(), y.max(), z.max()
xi, yi, zi = np.mgrid[xmin:xmax:30j, ymin:ymax:30j, zmin:zmax:30j]
coords = np.vstack([item.ravel() for item in [xi, yi, zi]])
density = kde(coords).reshape(xi.shape)
figure = mlab.figure('DensityPlot')
grid = mlab.pipeline.scalar_field(xi, yi, zi, density)
min = density.min()
max = density.max()
mlab.pipeline.volume(grid, vmin=min, vmax=min + 0.5 * (max - min))
mlab.axes()
mlab.show()
How to plot a 3D density map in python with matplotlib