在 Python 中可视化集群周围的 3D 外壳

问题描述 投票:0回答:1

问题 我有一个由 3D 坐标 (x,y,z) 组成的数据集,我想分别可视化“每个簇”的外壳,保留数据的复杂空间结构,例如以下示例:

我尝试过的:

matplotlib 的凸包:这似乎无法保留“孔”或环等空间结构,而是围绕所有给定的数据点构建凸包
  • mayavi 的 mlab:这似乎适用于连续函数,例如 np.sin(x)*np.cos(x)*np.sin(z) ,但不适用于像我这样的离散点数据集。
python matplotlib visualization cluster-analysis data-analysis
1个回答
0
投票
如何使用 matplotlib 在 python 中绘制 3D 密度图

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

这会产生:

我确信 Mayavi 可以做很多事情来美化这一点。

© www.soinside.com 2019 - 2024. All rights reserved.