Plot 3D surface in plotly using 3D data points extracted by meshgrid

Viewed 83

This is how one could plot the surface of a 3D sphere (2-Sphere) using spherical coordinates using plotly in python:

import numpy as np 
import plotly.graph_objects as go    

phi, theta = np.mgrid[0.0:np.pi:100j, 0.0:2.0 * np.pi:100j]
x = np.sin(phi) * np.cos(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(phi)

data = [
    go.Surface(x=x, y=y, z=z, colorscale='Electric', opacity=0.5)
]
fig = go.Figure(data=data)
fig.show()

I am interested in plotting a modified version of the sphere, where at each point (x,y,z) I need to add a scalar quantity (which depends on (x,y,z) and I can calculate using a function f(x,y,z)).

I think it might be impossible using the standard approach shown above (or at least I don't know how to do), but something that could be done is using scipy.interpolate.griddata as described in this answer to a similar question:

x = np.array(...)  # An array with all my x coordinates, of shape [1, N]
y = np.array(...)  # An array with all my y coordinates, of shape [1, N]
z = np.array(...)  # An array with all my z coordinates, of shape [1, N]

xi = np.linspace(x.min(), x.max(), 100)
yi = np.linspace(y.min(), y.max(), 100)

X,Y = np.meshgrid(xi,yi)

Z = griddata((x,y),z,(X,Y), method='cubic')

fig = go.Figure(go.Surface(x=xi,y=yi,z=Z))
fig.show()

However, I don't know how to extract my x,y,z-coordinates from that form:

phi, theta = np.mgrid[0.0:np.pi:100j, 0.0:2.0 * np.pi:100j]
x = np.sin(phi) * np.cos(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(phi)

Can you help? Thanks!

0 Answers
Related