How to draw ellipsoid with plotly

2024/7/7 7:27:10

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?

Answer

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
import numpy as np# some math: generate points on the surface of the ellipsoidphi = 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 notebookinit_notebook_mode()iplot([Mesh3d({'x': x.flatten(), 'y': y.flatten(), 'z': z.flatten(), 'alphahull': 0
})])

Ellipsoid

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) <- NULLell <- 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')))
https://en.xdnf.cn/q/73227.html

Related Q&A

PyTorch DataLoader uses same random seed for batches run in parallel

There is a bug in PyTorch/Numpy where when loading batches in parallel with a DataLoader (i.e. setting num_workers > 1), the same NumPy random seed is used for each worker, resulting in any random f…

How to fix 502 Bad Gateway Error in production(Nginx)?

When I tried to upload a big csv file of size about 600MB in my project which is hosted in the digital ocean, it tries to upload but shows 502 Bad Gateway Error (Nginx). The application is a data conve…

Shift theorem in Discrete Fourier Transform

Im trying to solve a problem with python+numpy in which Ive some functions of type that I need to convolve with another function . In order to optimize code, I performed the fft of f and g, I multipli…

Running pudb inside docker container

I prefer pudb for python debugging. I am building python applications that run inside docker container. Does any one know how to make pudb available inside docker container?Thank you

Argparse: defaults from file

I have a Python script which takes a lot of arguments. I currently use a configuration.ini file (read using configparser), but would like to allow the user to override specific arguments using command …

How can access Uploaded File in Google colab

Im new in python and I use Google Colab . I uploaded a train_data.npy into google Colab and then I want to use it . According to this link How to import and read a shelve or Numpy file in Google Colabo…

__add__ to support addition of different types?

Would be very easy to solve had python been a static programming language that supported overloading. I am making a class called Complex which is a representation of complex numbers (I know python has …

How to open .ndjson file in Python?

I have .ndjson file that has 20GB that I want to open with Python. File is to big so I found a way to split it into 50 peaces with one online tool. This is the tool: https://pinetools.com/split-files N…

loading a dataset in python (numpy) when there are variable spaces delimiting columns

I have a big dataset contains numeric data and in some of its rows there are variable spaces delimiting columns, like:4 5 6 7 8 9 2 3 4When I use this line:dataset=numpy.loadtxt("dataset.txt&q…

how to organise files with python27 app engine webapp2 framework

Ive gone through the getting started tut for python27 and app engine: https://developers.google.com/appengine/docs/python/gettingstartedpython27/By the end of the tut, all the the classes are in the sa…