# I want to plot a Sphere with Two Parallel Planes with PlotlyJS in 3D

**URL:** <https://discourse.julialang.org/t/i-want-to-plot-a-sphere-with-two-parallel-planes-with-plotlyjs-in-3d/93070>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [January 17, 2023, 6:06am UTC](https://discourse.julialang.org/t/i-want-to-plot-a-sphere-with-two-parallel-planes-with-plotlyjs-in-3d/93070 "2023-01-17T06:06:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [January 17, 2023, 6:06am UTC](https://discourse.julialang.org/t/i-want-to-plot-a-sphere-with-two-parallel-planes-with-plotlyjs-in-3d/93070/1 "2023-01-17T06:06:50Z")

</div>

Hi all,

I want to plot **a sphere and two planes** that are parallel to each other,

here is how it looks like from the slice of the sphere, the circle:  
 ![Capture d’écran_2023-01-17_13-02-57](https://global.discourse-cdn.com/julialang/original/3X/3/e/3e6fbc9a24b58dc12f720581c4edf887afb79346.png)

I try to read this:

> [@Plotting a 3D Surface](https://discourse.julialang.org/t/plotting-a-3d-surface/17143/2):
>
> It is not quite clear to me what the question is. However, you can use Plots as follows: using Plots; pyplot() x=range(-2,stop=2,length=100) y=range(sqrt(2),stop=2,length=100) f(x,y) = x\*y-x-y+1 plot(x,y,f,st=:surface,camera=(-30,30)) with result: [image] Here, st is short hand for seriestype (or something similar). Keyword camera sets the camera/viewing angles. I have used the default heatmap. If you want to plot contours, you can plot contour lines (st=:contour) or filled countours (st=:c…

this is my code and unable to plot the planes correctly:

```julia
using Plots; plotlyjs()

function sphere(r, C) # r: radius; C: center [cx,cy,cz]
    n = 100
    u = range(-π, π; length = n)
    v = range(0, π; length = n)
    x = C[1] .+ r*cos.(u) * sin.(v)'
    y = C[2] .+ r*sin.(u) * sin.(v)'
    z = C[3] .+ r*ones(n) * cos.(v)'
    return x, y, z
end

# the equation of a plane in 3-space: ax + by + cz = d
# at least one of the numbers a, b, c must be nonzero
# the plane as the graph of a function z of x and y: z = -(a/c)x + (-b/c) y + d/c

# Given points P, Q, R in space, find the equation of the plane through the 3 points.
# P = (1, 1, 1), Q = (1, 2, 0), R = (-1, 2, 1). 
# Solve with Gauss Elimination
# a + b + c = d
# a + 2b + 0c = d
# -a + 2b + c = d
function plane(x,y) 
    n = 100
    x=range(-2,stop=2,length = n)
    y=range(sqrt(2),stop=2,length = n)
    f(x,y) = -y + 5
    plot!(x,y,f,st=:surface,c=my_cg,camera=(-30,30))
end

my_cg = cgrad([:red,:blue])
surface(sphere(2, [1,2,3]), c=my_cg)
surface(plane(0,1))

# using LinearAlgebraX
#A = [1 2 3 1;
# 0.5 2 3 1;
# 1.5 2 3 1]
# rrefx(A)
# Solution $a=0, b=1, c=1, d=5$

#Change result to Float
# Float64.(rrefx(A))

```

I have made an improvement a bit, if I could finish it alone I will post it here.

![Capture d’écran_2023-01-17_14-38-02](https://global.discourse-cdn.com/julialang/original/3X/8/3/834f85961928f489665e2fb7c4a2f8d340af93ee.png)

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [January 17, 2023, 7:48am UTC](https://discourse.julialang.org/t/i-want-to-plot-a-sphere-with-two-parallel-planes-with-plotlyjs-in-3d/93070/2 "2023-01-17T07:48:27Z")

</div>

I made it better, but let see if anyone can fix or beautify this plot of mine:

```julia
using Plots; plotlyjs()

function sphere(r, C) # r: radius; C: center [cx,cy,cz]
    n = 100
    u = range(-π, π; length = n)
    v = range(0, π; length = n)
    x = C[1] .+ r*cos.(u) * sin.(v)'
    y = C[2] .+ r*sin.(u) * sin.(v)'
    z = C[3] .+ r*ones(n) * cos.(v)'
    return x, y, z
end

# the equation of a plane in 3-space: ax + by + cz = d
# at least one of the numbers a, b, c must be nonzero
# the plane as the graph of a function z of x and y: z = -(a/c)x + (-b/c) y + d/c

# Given points P, Q, R in space, find the equation of the plane through the 3 points.
# P = (1, 1, 1), Q = (1, 2, 0), R = (-1, 2, 1). 
# Solve with Gauss Elimination
# a + b + c = d
# a + 2b + 0c = d
# -a + 2b + c = d
function plane(x,y) 
    n = 100
    x=range(-3,stop=5,length = n)
    y=range(1,stop=5,length = n)
    f(x,y) = -y + 4.5
    my_cg2 = cgrad([:green,:orange])
    plot!(x,y,f,st=:surface,c=my_cg2,camera=(-30,30))
end

function plane2(x,y) 
    n = 100
    x=range(-3,stop=5,length = n)
    y=range(1,stop=5,length = n)
    f(x,y) = -y + 6
    my_cg2 = cgrad([:green,:orange])
    plot!(x,y,f,st=:surface,c=my_cg2,camera=(-30,30))
end

my_cg = cgrad([:red,:blue])
surface(sphere(2, [1,2,3]), c=my_cg)
surface(plane(0,1))
surface(plane2(0,1))

# using LinearAlgebraX
#A = [1 2 3 1;
# 0.5 2 3 1;
# 1.5 2 3 1]
# rrefx(A)
# Solution $a=0, b=1, c=1, d=5$

#Change result to Float
# Float64.(rrefx(A))

```

![Capture d’écran_2023-01-17_14-47-32](https://global.discourse-cdn.com/julialang/original/3X/1/d/1da7b30eb5e934c4d90bc4b88e371d6595bc92ec.png)
