# Plotting a Sphere with Inputs of Radius and center

**URL:** https://discourse.julialang.org/t/plotting-a-sphere-with-inputs-of-radius-and-center/85978
**Category:** General Usage
**Tags:** plotting, plots, plotlyjs
**Created:** [August 19, 2022, 7:36am UTC](https://discourse.julialang.org/t/plotting-a-sphere-with-inputs-of-radius-and-center/85978 "2022-08-19T07:36:24Z")
**Posts on this page:** 5
**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: [August 19, 2022, 7:36am UTC](https://discourse.julialang.org/t/plotting-a-sphere-with-inputs-of-radius-and-center/85978/1 "2022-08-19T07:36:24Z")

</div>

Hi all,

I read this forum:

> [@Unable to plot a sphere surface using Plots package](https://discourse.julialang.org/t/unable-to-plot-a-sphere-surface-using-plots-package/60194/7):
>
> Thanks a lot!!! Actually you are 100% correct. It looks like i had installed a pacakge FourierAnalysis which forced my Pkg to install a very old version of Plots, and wouldn’t allow me to upgrade it. I removed the FourierAnalysis package and finally was able to update plots, and the axes are now correct. Thanks!! I think I understand now why I should activate a new environment in each new project directory! Thanks

I want to know whether there is a way to plot a sphere based on an inputs of:

1. radius
2. center of the sphere (x,y,z)

Instead of using sin,cos inputs.

I use this code from the forum:

```julia
using Plots

n = 100
u = range(-π, π; length = n)
v = range(0, π; length = n)
x = cos.(u) * sin.(v)'
y = sin.(u) * sin.(v)'
z = ones(n) * cos.(v)'

plotly()
surface(x, y, z)

```

Plotly() is great it is very interactive.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 19, 2022, 8:29am UTC](https://discourse.julialang.org/t/plotting-a-sphere-with-inputs-of-radius-and-center/85978/2 "2022-08-19T08:29:16Z")

</div>

Would it be OK to define a `sphere` function:

```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

surface(sphere(2, [1,2,3]))

```

---

<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: [August 19, 2022, 11:23am UTC](https://discourse.julialang.org/t/plotting-a-sphere-with-inputs-of-radius-and-center/85978/3 "2022-08-19T11:23:53Z")

</div>

Wow it is great,

I just want to know the mathematics theory / formula for the

```julia
x = C[1] .+ r*cos.(u) * sin.(v)'
y = C[2] .+ r*sin.(u) * sin.(v)'
z = C[3] .+ r*ones(n) * cos.(v)'

```

What is it called? Parametric equations?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 19, 2022, 11:42am UTC](https://discourse.julialang.org/t/plotting-a-sphere-with-inputs-of-radius-and-center/85978/4 "2022-08-19T11:42:48Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> What is it called? Parametric equations?

Yes, parametric equations of the sphere using spherical coordinates (radial, polar and azimuthal).

Btw, in the post you linked there is a [solution using Cartesian coordinates](https://discourse.julialang.org/t/unable-to-plot-a-sphere-surface-using-plots-package/60194/8), but more tricky to stitch.

---

<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: [August 19, 2022, 12:05pm UTC](https://discourse.julialang.org/t/plotting-a-sphere-with-inputs-of-radius-and-center/85978/5 "2022-08-19T12:05:14Z")

</div>

I tried that code too, but there is a black line in the middle of the sphere when using `gr()`

 ![Capture d’écran_2022-08-19_19-02-01](https://global.discourse-cdn.com/julialang/original/3X/2/5/255a1415044dfc6d04a17231a22ee5921c7bfbb3.png)

I learn the parametric equations so I know the mathematical science behind how to create sphere:

```julia
u = range(-π, π; length = n)

```

This has range of `-π, π`, while out there a source use range of `0, 2π`  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/8/a8a80375da958dea6346603e6e101fcee24544ab.png)

![image](https://global.discourse-cdn.com/julialang/original/3X/d/8/d8e51a85f48b7499a222da538522c22489504a62.png)
