在使用Plotly绘制3D凸包图。

huangapple go评论73阅读模式
英文:

Plot convex-hull in 3D using plotly

问题

我正在尝试使用plotly来绘制一组点的三维凸包。我使用了Mesh3d对象,但表面没有正确创建(请参见下面的图片)。我该如何修复这个问题?

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

当前输出是:

在使用Plotly绘制3D凸包图。

英文:

I am trying to use plotly to plot the 3D convex-hull of a set of points. I am using Mesh3d objects but the surfaces are not created correctly (see the picture below). 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:

在使用Plotly绘制3D凸包图。

答案1

得分: 2

I think what you miss is the alphahull option.

If you want a convex hull you need to add 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

将给你想要的立方体:

在使用Plotly绘制3D凸包图。


<details>
<summary>英文:</summary>

I think what you miss is the [alphahull](https://plotly.com/python/reference/#mesh3d-alphahull) option.

If you want a convex hull you need to add `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:

![resutl][1]

[1]: https://i.stack.imgur.com/WuizR.png

</details>



huangapple
  • 本文由 发表于 2020年1月4日 00:04:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定