问题 我有一个由 3D 坐标 (x,y,z) 组成的数据集,我想分别可视化“每个簇”的外壳,保留数据的复杂空间结构,例如以下示例:
我尝试过的:matplotlib 的凸包:这似乎无法保留“孔”或环等空间结构,而是围绕所有给定的数据点构建凸包
和 使用 Mayavi / Python 根据数据绘制 3D 等高线图的答案,是使用高斯 KDE 并生成输出的 3D 等高线图使用 mayavi:
from scipy.stats import gaussian_kde
from mayavi.mlab import contour3d
import numpy as np
# generate some random points
npoints = 10_000
x = np.random.rand(npoints)
y = np.sin(2 * np.pi * x * 0.75 + 0.1) + 0.4 * np.random.randn(len(x))
z = np.cos(2 * np.pi * x * 0.3 + 0.6) + np.sin(2 * np.pi * y) + 0.4 * np.random.randn(len(x))
# construct a Gaussian KDE
xyz = np.vstack([x, y, z])
kde = gaussian_kde(xyz)
# evaluate the KDE on a grid
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)
# create the contour plot (for some reason contours must be at least
# 3 to show anything!)
contour3d(xi, yi, zi, density, contours=3, transparent=True)
这会产生: