10

I have a cloud of points in three-dimensional space, and have estimated some distribution over those points (also in 3D space; using kernel density estimation although that's irrelevant for this question). I would like to plot the projection of that distribution as a contour plot onto all three axes (x, y, and z). It is straightforward to do this for the z-axis (i.e. project onto plane with same z-coordinate everywhere):

import numpy as np
import scipy as sp
import scipy.stats
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

# generate some points of a 3D Gaussian
points = np.random.normal(size=(3, 50))

# do kernel density estimation to get smooth estimate of distribution
# make grid of points
x, y, z = np.mgrid[-4:4:100j, -4:4:100j, -4:4:100j]
kernel = sp.stats.gaussian_kde(points)
positions = np.vstack((x.ravel(), y.ravel(), z.ravel()))
density = np.reshape(kernel(positions).T, x.shape)

# now density is 100x100x100 ndarray

# plot points
ax = plt.subplot(projection='3d')
ax.plot(points[0,:], points[1,:], points[2,:], 'o')

# plot projection of density onto z-axis
plotdat = np.sum(density, axis=2)
plotdat = plotdat / np.max(plotdat)
plotx, ploty = np.mgrid[-4:4:100j, -4:4:100j]
ax.contour(plotx, ploty, plotdat, offset=-4)

ax.set_xlim((-4, 4))
ax.set_ylim((-4, 4))
ax.set_zlim((-4, 4))

Projection of contours onto z-axis

However, doing this for the other axes seems to be not implemented in Matplotlib. If I use the method outlined in this example, and specify a zdir keyword argument:

# plot projection of density onto x-axis
plotdat = np.sum(density, axis=0)
plotdat = plotdat / np.max(plotdat)
ploty, plotz = np.mgrid[-4:4:100j, -4:4:100j]
ax.contour(ploty, plotz, plotdat, offset=-4, zdir='x')

the generation of the contour is done 'along another slice', so to say:

enter image description here

Whereas I want something like this (bad Paint skills; hope the idea is clear):

enter image description here

One option I had in mind was to generate the contour along the default zdir='z' and then rotate the resulting curves in 3D space, but I have no idea how to approach this. I would be very grateful for any pointers!

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
EelkeSpaak
  • 2,757
  • 2
  • 17
  • 37

1 Answers1

12

I tried to modify the contour plots by mixing up the data calculated as a sum along an axis with the grid created by np.mgrid. I calculated the sum of the density along the axis on which I want to have the contour. This looks as follows:

# plot projection of density onto z-axis
plotdat = np.sum(density, axis=2)
plotdat = plotdat / np.max(plotdat)
plotx, ploty = np.mgrid[-4:4:100j, -4:4:100j]
ax.contour(plotx, ploty, plotdat, offset=-4, zdir='z')

#This is new
#plot projection of density onto y-axis
plotdat = np.sum(density, axis=1) #summing up density along y-axis
plotdat = plotdat / np.max(plotdat)
plotx, plotz = np.mgrid[-4:4:100j, -4:4:100j]
ax.contour(plotx, plotdat, plotz, offset=4, zdir='y')

#plot projection of density onto x-axis
plotdat = np.sum(density, axis=0) #summing up density along z-axis
plotdat = plotdat / np.max(plotdat)
ploty, plotz = np.mgrid[-4:4:100j, -4:4:100j]
ax.contour(plotdat, ploty, plotz, offset=-4, zdir='x')
#continue with your code

Unfortunately I'm not very familiar with the kernel density estimation, so I hope I did not understand something completely wrong but the result generated if you add the few lines of code above looks something similar than your fancy paint picture :) enter image description here

jammartin
  • 304
  • 6
  • 11
  • So simple, I feel stupid for not having thought of that myself :) Excellent answer; thanks a lot! – EelkeSpaak Apr 19 '16 at 08:58
  • You're welcome! These meshgrids needed for matplotlib's 3D plotting often confuse myself too. Glad to hear that I could help you! :) – jammartin Apr 19 '16 at 09:19