【发布时间】:2022-10-09 17:45:09
【问题描述】:
我有来自第三方数据源的 3D 顶点。 plotly Volume 对象期望所有坐标为一维列表。他们网站上的示例使用 mgrid 函数将 3D 空间填充到 flatten 函数中,以获取每个轴的 1D 列表。 https://plotly.com/python/3d-volume-plots/
我不明白为什么我的方法会产生一个空的情节。
coords 是我的 (N, 3) 形状的顶点列表。
请参阅以下代码 sn-p 绘制随机坐标,对它们进行排序,但结果为空渲染。
X = np.random.uniform(0, 1, 30000)
Y = np.random.uniform(0, 1, 30000)
Z = np.random.uniform(0, 1, 30000)
coords = np.dstack((X.flatten(), Y.flatten(), Z.flatten()))[0]
sort_idx = np.lexsort((coords[:, 0], coords[:, 1], coords[:, 2]))
coords = coords[sort_idx]
X=coords[:, 0]
Y=coords[:, 1]
Z=coords[:, 2]
V = np.sin(X) * np.sin(Y) + Z
fig = go.Figure(data=go.Volume(
x=X,
y=Y,
z=Z,
value=V,
isomin=np.min(Z),
isomax=np.max(Z),
opacity=0.1, # needs to be small to see through all surfaces
surface_count=20, # needs to be a large number for good volume rendering
colorscale='Spectral',
reversescale=True
))
fig.show()
更新:似乎 plotly 期望对坐标进行排序。
X, Y, Z = np.mgrid[-50:50:40j, -50:50:40j, -8:8:10j]
coords = np.dstack((X.flatten(), Y.flatten(), Z.flatten()))[0]
np.random.shuffle(coords)
像这样打乱列表并将coords 插入上面的代码会产生一个空的Volumn 渲染。
我现在尝试对我的数据点进行排序,但我仍然得到一个空渲染。如何共享我的数据集? npfile,但我应该在哪里托管它?
sort_idx = np.lexsort((coords[:, 0], coords[:, 1], coords[:, 2]))
coords = coords[sort_idx]
更新 2:使用均匀随机分布生成坐标会导致顶点列表似乎无法通过 plotly 处理甚至排序后。
X = np.random.uniform(0, 1, 30000)
Y = np.random.uniform(0, 1, 30000)
Z = np.random.uniform(0, 1, 30000)
coords = np.dstack((X.flatten(), Y.flatten(), Z.flatten()))[0]
【问题讨论】:
-
关键的答案是我们为什么要在构建体积时使用网格?!