Spherical Cap in Julia

This is the code for the cap plot:

using PlotlyJS

R = 1
a = 40*π/180  # consider the sphere cap for 
N= 100
θ = collect(LinRange(0, 2π, N))
ϕ = collect(LinRange(0, a,  N)) 
xs = R*cos.(θ)*sin.(ϕ)'
ys = R*sin.(θ)*sin.(ϕ)'
zs = ones(N)*cos.(ϕ)';
n= 400
R=1.01
u = 0.5* (1-cos(a))*rand(n)
ϕ = acos.(1 .- 2*u)
θ = 2π*rand(n)
x = R*cos.(θ) .* sin.(ϕ)
y = R*sin.(θ) .* sin.(ϕ)
z=  R*cos.(ϕ)
pl = Plot([surface(x=xs, y=ys, z=zs, colorscale= [[0, "#555555"], [1, "#555555"]], showscale=false),
           scatter3d(x=x, y=y, z=z, mode="markers", marker_size=3)],
           Layout(width=600,  scene_aspectmode="data", scene_camera_eye=attr(x=1.75, y=1.75, z=0.9)))
display(pl)

scene_aspectmode="data" makes the difference and does not let to be displayed a deformed cap.

If you want to use a real colorscale for the spherical cap, not just a single color, replace the plot definition by:

pl = Plot([surface(x=xs, y=ys, z=zs, colorscale= colors.matter, reversescale=true, showscale=false),
           scatter3d(x=x, y=y, z=z, mode="markers", marker_size=3, marker_color="black")],
           Layout(width=600,  scene_aspectmode="data", scene_camera_eye=attr(x=1.75, y=1.75, z=0.9)))
1 Like