在 3D 图中绘制所有三个轴上的分布轮廓

2024-01-05

我在三维空间中有一个点云,并估计了这些点的一些分布(也在 3D 空间中;使用核密度估计 http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gaussian_kde.html尽管这与这个问题无关)。我想将该分布的投影绘制为所有三个轴(x、y 和 z)上的等值线图。对于 z 轴执行此操作很简单(即投影到各处具有相同 z 坐标的平面上):

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))

然而,对其他轴执行此操作似乎没有在 Matplotlib 中实现。如果我使用中概述的方法这个例子 http://matplotlib.org/examples/mplot3d/contour3d_demo3.html,并指定一个zdir关键字参数:

# 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')

轮廓的生成是“沿着另一个切片”完成的,也就是说:

而我想要这样的东西(糟糕的绘画技巧;希望这个想法很清楚):

我想到的一个选择是沿着默认值生成轮廓zdir='z'然后在 3D 空间中旋转生成的曲线,但我不知道如何解决这个问题。我将非常感谢任何指点!


我尝试通过将计算为沿轴总和的数据与创建的网格混合来修改等高线图np.mgrid。我计算了沿我想要轮廓的轴的密度总和。这看起来如下:

# 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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 3D 图中绘制所有三个轴上的分布轮廓 的相关文章

随机推荐