如何用情节画椭圆体

Ily*_*rov 2 python 3d r plotly r-plotly

有什么方法可以使用可绘制的3D绘制类似椭球的表面吗?

目前在文档中仅讨论z = f(x,y)形式的曲面。还有Mesh 3D,但我没有找到任何示例。似乎可以手动进行椭球的三角剖分,然后使用“网格”获取椭球,但这对我来说似乎有点困难。有什么更好的方法吗?

Ily*_*rov 5

好吧,这比我想象的要容易。有一个alphahull选项要求绘图自动计算相应的三角剖分。

from plotly.offline import iplot, init_notebook_mode
from plotly.graph_objs import Mesh3d
from numpy import sin, cos, pi

# some math: generate points on the surface of ellipsoid

phi = np.linspace(0, 2*pi)
theta = np.linspace(-pi/2, pi/2)
phi, theta=np.meshgrid(phi, theta)

x = cos(theta) * sin(phi) * 3
y = cos(theta) * cos(phi) * 2
z = sin(theta)

# to use with Jupyter notebook

init_notebook_mode()

iplot([Mesh3d({
                'x': x.flatten(), 
                'y': y.flatten(), 
                'z': z.flatten(), 
                'alphahull': 0
})])
Run Code Online (Sandbox Code Playgroud)

椭圆体

这是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'))
Run Code Online (Sandbox Code Playgroud)

编辑:也可以用来type='surface'生成参数图:在这种情况下,必须提供二维xy

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')))
Run Code Online (Sandbox Code Playgroud)


Sté*_*ent 5

假设椭球体由方程 给出(X-c)'A(X-c) = r

library(Rvcg)
sphr <- vcgSphere()
library(rgl)
ell <- scale3d(transform3d(sphr, chol(A)), r, r, r)
vs <- ell$vb[1:3,] + c
idx <- ell$it - 1
library(plotly)
p <- plot_ly(type="mesh3d",
  x = vs[1,], y = vs[2,], z = vs[3,],
  i = idx[1,], j = idx[2,], k = idx[3,],
  opacity = 0.3) 
Run Code Online (Sandbox Code Playgroud)