简体   繁体   English

使用 plotly 在 3D 中绘制凸包

[英]Plot convex-hull in 3D using plotly

I am trying to use plotly to plot the 3D convex-hull of a set of points.我正在尝试使用plotly绘制一组点的 3D 凸包。 I am using Mesh3d objects but the surfaces are not created correctly (see the picture below).我正在使用Mesh3d对象,但表面创建不正确(见下图)。 How can I fix this?我怎样才能解决这个问题?

import plotly.graph_objects as go
import itertools, math, numpy as np

from scipy.spatial import ConvexHull

# This simply creates a set of points:
n = 3
m = 3
E = np.array(list(itertools.product(np.arange(-1, 1.1, .5), repeat=m)))
V = [
 [-2.20676418,  1.53670924, -1.5541674 ],
 [ 0.63437404,  0.07306301,  3.82253086],
 [ 3.19989112,  0.71987311,  2.79373418]
]
x = np.array([np.dot(V, e) for e in E])

# Then I compute the convex hull using scipy:
xc = x[ConvexHull(x).vertices]

fig = go.Figure()
fig.add_trace(go.Mesh3d(x=xc[:, 0], y=xc[:, 1], z=xc[:, 2], color="blue", opacity=.5))
fig

Current output is:当前输出为:

在此处输入图片说明

I think what you miss is the alphahull option.我认为您错过的是alphahull选项。

If you want a convex hull you need to add alphahull=0 :如果你想要一个凸包,你需要添加alphahull=0

import plotly.graph_objects as go
import itertools, math, numpy as np

from scipy.spatial import ConvexHull

# This simply creates a set of points:
n = 3
m = 3
E = np.array(list(itertools.product(np.arange(-1, 1.1, .5), repeat=m)))
V = [
 [-2.20676418,  1.53670924, -1.5541674 ],
 [ 0.63437404,  0.07306301,  3.82253086],
 [ 3.19989112,  0.71987311,  2.79373418]
]
x = np.array([np.dot(V, e) for e in E])

# Then I compute the convex hull using scipy:
xc = x[ConvexHull(x).vertices]

fig = go.Figure()
fig.add_trace(go.Mesh3d(x=xc[:, 0], 
                        y=xc[:, 1], 
                        z=xc[:, 2], 
                        color="blue", 
                        opacity=.5,
                        alphahull=0))
fig

will give you the vube you want:会给你你想要的vube:

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM