How to plot in Julia over the surface of a sphere?

43 Views Asked by At

I have a function let's say f(\theta, \phi), that associate to each point of a sphere a value between 0 and 1, now I want my plot to show a sphere, and then the values of this function over the surface of the sphere with color to indicate the value of the function. The function not necessarly cover the full sphere, sometimes is just a quadrant and sometimes a path. How can i do it? I usually use jupiter on vscode.

Thank you!

I can plot the sphere but I can't figure how to cover it with the function value. I tried with PyPlot but I have problem in showing the graph interactively so maybe Plots is better

1

There are 1 best solutions below

0
Stéphane Laurent On

You can use the packages Meshes and MeshViz to do and plot the mesh. There's an argument color in MeshViz.viz, which can be a vector representing a color for each vertex. Once you have your number f(theta,phi) in [0,1], you can map it to a color with the ColorSchemes package.

using Meshes
using MeshViz
using GLMakie
using ColorSchemes

# parameterization of the sphere
function sphere(theta, phi)
    [cos(theta)*sin(phi), sin(theta)*sin(phi), cos(phi)]
end

# coloring function - taking values in [0, 1]
function f(theta, phi)
    theta / (2*pi)
end

# make mesh
nu = 30
nv = 20
theta_ = LinRange(0, 2*pi, nu+1)[1:nu]
phi_ = LinRange(0, pi, nv+1)[1:nv]
points = [tuple(sphere(theta, phi)...) for theta in theta_ for phi in phi_]
topo = GridTopology((nv, nu), (true, true))
mesh = SimpleMesh(Meshes.Point.(points), topo)

# colors 
u_ = [f(theta, phi) for theta in theta_ for phi in phi_]
colors = [get(ColorSchemes.buda, u) for u in u_]

# draw the sphere
MeshViz.viz(mesh; color = colors)