# Unable to plot a sphere surface using Plots package

**URL:** <https://discourse.julialang.org/t/unable-to-plot-a-sphere-surface-using-plots-package/60194>\
**Category:** Visualization\
**Tags:** plotting, plots\
**Created:** [April 28, 2021, 6:49pm UTC](https://discourse.julialang.org/t/unable-to-plot-a-sphere-surface-using-plots-package/60194 "2021-04-28T18:49:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AAlshammaa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aalshammaa/32/23499_2.png) [@AAlshammaa](https://discourse.julialang.org/u/AAlshammaa)\
**Post date:** [April 28, 2021, 6:49pm UTC](https://discourse.julialang.org/t/unable-to-plot-a-sphere-surface-using-plots-package/60194/1 "2021-04-28T18:49:18Z")

</div>

I am trying to follow a tutorial on youtube for SVD, and the instructor created a sphere using Python. I am trying to translate the code to Julia

```julia
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)'

surface(x, y, z)

```

But i get this error

```julia
Failed to show value:
MethodError: Cannot `convert` an object of type Plots.Surface{Matrix{Float64}} to an object of type Vector{Float64}

```

However when i use `plot(x, y, z)` it plots a hundred circles  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/b/6b2f8961d9081640c5389f2c991594c7f727c720.jpeg)

The original code is from [this video](https://youtu.be/MJAvyt9v0g4?t=204)  
How can I plot a sphere surface?  
Thanks

---

<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:** [April 28, 2021, 10:07pm UTC](https://discourse.julialang.org/t/unable-to-plot-a-sphere-surface-using-plots-package/60194/2 "2021-04-28T22:07:23Z")

</div>

Using Plots.jl plotly() backend, see this [solution](https://discourse.julialang.org/t/drawing-parametric-surfaces-in-plots-jl/36560/9).

---

<div class="post-metadata">

**Author:** ![AAlshammaa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aalshammaa/32/23499_2.png) [@AAlshammaa](https://discourse.julialang.org/u/AAlshammaa)\
**Post date:** [April 29, 2021, 1:22am UTC](https://discourse.julialang.org/t/unable-to-plot-a-sphere-surface-using-plots-package/60194/3 "2021-04-29T01:22:05Z")

</div>

its not possible with GR backend?

---

<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:** [April 29, 2021, 7:48am UTC](https://discourse.julialang.org/t/unable-to-plot-a-sphere-surface-using-plots-package/60194/4 "2021-04-29T07:48:46Z")

</div>

Do not think that gr() handles parametric surfaces in 3D (_Makie is best for that_).

---

<div class="post-metadata">

**Author:** ![AAlshammaa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aalshammaa/32/23499_2.png) [@AAlshammaa](https://discourse.julialang.org/u/AAlshammaa)\
**Post date:** [April 29, 2021, 2:22pm UTC](https://discourse.julialang.org/t/unable-to-plot-a-sphere-surface-using-plots-package/60194/5 "2021-04-29T14:22:40Z")

</div>

Thank you. I used plotly() with the same code, but my plot’s ticks are all zeros. Do i have to manually set the ticks? Why it couldn’t be done automatically, or am I missing something?

```julia
# using Pkg; Pkg.add("Plotly")
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)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/3/4/34a1f3701879c1a6b8331084100f79405b8ce474.png)

---

<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:** [April 29, 2021, 2:25pm UTC](https://discourse.julialang.org/t/unable-to-plot-a-sphere-surface-using-plots-package/60194/6 "2021-04-29T14:25:16Z")

</div>

This might be widespread issue introduced in Julia 1.6.1 while using older Plots.jl versions.

---

<div class="post-metadata">

**Author:** ![AAlshammaa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aalshammaa/32/23499_2.png) [@AAlshammaa](https://discourse.julialang.org/u/AAlshammaa)\
**Post date:** [April 29, 2021, 2:34pm UTC](https://discourse.julialang.org/t/unable-to-plot-a-sphere-surface-using-plots-package/60194/7 "2021-04-29T14:34:38Z")

</div>

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

---

<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:** [April 29, 2021, 8:54pm UTC](https://discourse.julialang.org/t/unable-to-plot-a-sphere-surface-using-plots-package/60194/8 "2021-04-29T20:54:23Z")

</div>

Just for the record, a decent sphere can be produced with Plots.jl gr() back-end by splitting it in two (_top and bottom surfaces)_. However, it requires a careful computation of the end points for a nice stitching job and minding the plotting order.

```julia
using Plots; gr()

zplus(x,y) = (x^2 + y^2) <= 1 ? sqrt.(abs.(1.0 .- x.^2 .- y.^2)) : NaN
zminus(x,y) = (x^2 + y^2) <= 1 ? -sqrt.(abs.(1.0 .- x.^2 .- y.^2)) : NaN

x = collect(-1:0.01:1);
y = @. (1-x^2)^0.5
y = reshape(permutedims([-y y]),:,1)[:]
x = sort([x; x.+eps()])

surface(x,y,zminus, ratio=:equal,xlims=(-1,1),ylims=(-1,1),zlims=(-1,1)) # isometric
surface!(x,y,zplus, ratio=:equal,xlims=(-1,1),ylims=(-1,1),zlims=(-1,1)) # isometric

```

![Plots_gr_sphere_2_halves](https://global.discourse-cdn.com/julialang/original/3X/4/c/4c8515cf9b9d0cbece9acebd7f5bba36911b487a.png)
