好吧,比我想象的要容易。有alphahull
要求绘图自动计算相应三角剖分的选项。
from plotly.offline import iplot, init_notebook_mode
from plotly.graph_objs import Mesh3d
import numpy as np
# some math: generate points on the surface of the ellipsoid
phi = np.linspace(0, 2*pi)
theta = np.linspace(-pi/2, pi/2)
phi, theta=np.meshgrid(phi, theta)
x = np.cos(theta) * np.sin(phi) * 3
y = np.cos(theta) * np.cos(phi) * 2
z = np.sin(theta)
# to use with Jupyter notebook
init_notebook_mode()
iplot([Mesh3d({
'x': x.flatten(),
'y': y.flatten(),
'z': z.flatten(),
'alphahull': 0
})])
这是 R 版本:
library(pracma)
theta <- seq(-pi/2, pi/2, by=0.1)
phi <- seq(0, 2*pi, by=0.1)
mgrd <- meshgrid(phi, theta)
phi <- mgrd$X
theta <- mgrd$Y
x <- cos(theta) * cos(phi) * 3
dim(x) <- NULL
y <- cos(theta) * sin(phi) * 2
dim(y) <- NULL
z <- sin(theta) * scale
dim(z) <- NULL
ell <- cbind(x, y, z)
ell <- setNames(ell, c('x', 'y', 'z'))
library(plotly)
p <- plot_ly(as.data.frame(ell), x=x, y=y, z=z, type='mesh3d', alphahull = 0)
p %>% layout(scene = list(aspectmode = 'data'))
编辑:也可以使用type='surface'
生成参数图:在这种情况下,必须提供二维x
and y
.
library(plotly)
library(pracma)
mgrd <- meshgrid(seq(-pi, pi, length.out = 100), seq(-pi/2, pi/2, length.out = 100))
U <- mgrd$X
V <- mgrd$Y
frame <- list(x=cos(V)*cos(U)*3, y=cos(V)*sin(U)*2, z=sin(V))
plot_ly(frame, type='surface', x=x, y=y, z=z, showlegend=F, showscale=F,
colorscale=list(list(0, 'blue'), list(1, 'blue')))