如何绘制椭圆体

时间:2016-03-06 00:20:49

标签: python r 3d plotly r-plotly

有没有办法用椭圆体绘制像椭圆体这样的表面?

目前,文档中仅讨论了 z = f(x,y)形式的表面。还有Mesh 3D,但我找不到它的例子。似乎可以手动对椭球进行三角测量,然后使用网格获得椭球,但对我来说看起来有点困难。有没有更好的方法呢?

3 个答案:

答案 0 :(得分: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
})])

Ellipsoid

这是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'生成参数图:在这种情况下,必须提供二维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')))

答案 1 :(得分:2)

为什么不从椭圆体上的数学项中抓取这个方程中的z求解:

enter image description here

require(plotly)
a=5; b=7; c=9
x=rep(seq(-10,10,by=1), each=21)
y=rep( seq(-10,10,by=1), times=21)
z <- c^2*sqrt(1-x^2/a^2-y^2/b^2)
#Warning message:
#In sqrt(1 - x^2/a^2 - y^2/b^2) : NaNs produced

 plot_ly(z = matrix(z,21,21), type = "surface")

enter image description here

答案 2 :(得分:2)

假设椭圆由方程compactMap给出。

Optional<Any>