如何用plotly绘制椭圆体 [英] How to draw ellipsoid with plotly

查看:29
本文介绍了如何用plotly绘制椭圆体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以用可绘制的 3D 绘制类似椭球的曲面吗?

目前文档中只讨论了 z=f(x,y) 形式的曲面.还有

这是R版本:

库(pracma)theta <- seq(-pi/2, pi/2, by=0.1)phi <- seq(0, 2*pi, by=0.1)mgrd <- meshgrid(p theta)phi <- mgrd$Xtheta <- mgrd$Yx <- cos(theta) * cos(phi) * 3暗淡(x)<- NULLy <- cos(theta) * sin(phi) * 2暗淡(y)<- NULLz <- sin(theta) * 比例暗淡(z)<- NULLell <- cbind(x, y, z)ell <- setNames(ell, c('x', 'y', 'z'))图书馆(情节)p <- plot_ly(as.data.frame(ell), x=x, y=y, z=z, type='mesh3d', alphahull = 0)p %>% 布局(场景 = 列表(aspectmode = '数据'))

<小时>

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

图书馆(情节)图书馆(实用)mgrd <-meshgrid(seq(-pi, pi, length.out = 100), seq(-pi/2, pi/2, length.out = 100))U <- mgrd$XV <- mgrd$Y框架<-列表(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')))

Are there any way to plot a surface like ellipsoid with plotly 3D?

Currently only surfaces of the form z=f(x,y) are discussed in the docs. There is also Mesh 3D, but I found no examples for it. It seem to be possible to make a triangulation of ellipsoid manually and then use Mesh to get ellipsoid, but it looks a bit difficult for me. Are there any better way to do it?

解决方案

Okay, it is easier than I thought. There is alphahull option that asks plotly to calculate the corresponding triangulation automatically.

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
})])

And this is R version:

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'))


EDIT: it is also possible to use type='surface' to produce parametric plots: in this case one have to provide two-dimensional 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')))

这篇关于如何用plotly绘制椭圆体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆