# Use Plots() library to graph a colored surface

**URL:** <https://discourse.julialang.org/t/use-plots-library-to-graph-a-colored-surface/66561>\
**Category:** General Usage\
**Created:** [August 17, 2021, 5:36pm UTC](https://discourse.julialang.org/t/use-plots-library-to-graph-a-colored-surface/66561 "2021-08-17T17:36:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Norbert-droid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/norbert-droid/32/20364_2.png) [@Norbert-droid](https://discourse.julialang.org/u/Norbert-droid)\
**Post date:** [August 17, 2021, 5:36pm UTC](https://discourse.julialang.org/t/use-plots-library-to-graph-a-colored-surface/66561/1 "2021-08-17T17:36:24Z")

</div>

I want to make a surface in 3D and color the surface using a fourth coordinate. I want to do this task using Plots and Plotly to rotate the graph at all angles. Here I put examples of the surfaces I want:

![im1](https://global.discourse-cdn.com/julialang/original/3X/2/0/20de6eba4a7d1c4836a4347e0841cb8d071923b8.png)  
 ![im2](https://global.discourse-cdn.com/julialang/original/3X/a/c/ace2044ed4af89d583eda034524713b272a59926.png)  
 ![im3](https://global.discourse-cdn.com/julialang/original/3X/d/4/d4f554a751743d3a10319ae24ddad09dd9ab6145.png)

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 17, 2021, 5:41pm UTC](https://discourse.julialang.org/t/use-plots-library-to-graph-a-colored-surface/66561/2 "2021-08-17T17:41:28Z")

</div>

Just to double check, you don’t accept a solution with Makie.jl? BeautifulMakie has various examples: [https://lazarusa.github.io/BeautifulMakie/](https://lazarusa.github.io/BeautifulMakie/)

For instance: [https://lazarusa.github.io/BeautifulMakie/GeoPlots/](https://lazarusa.github.io/BeautifulMakie/GeoPlots/)

---

<div class="post-metadata">

**Author:** ![Norbert-droid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/norbert-droid/32/20364_2.png) [@Norbert-droid](https://discourse.julialang.org/u/Norbert-droid)\
**Post date:** [August 17, 2021, 5:59pm UTC](https://discourse.julialang.org/t/use-plots-library-to-graph-a-colored-surface/66561/3 "2021-08-17T17:59:34Z")

</div>

I didn’t know the library before, I’ll give it a try. But I want to dominate the library Plots first.

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [August 17, 2021, 7:53pm UTC](https://discourse.julialang.org/t/use-plots-library-to-graph-a-colored-surface/66561/4 "2021-08-17T19:53:25Z")

</div>

with PlotlyJS.jl you can color a surface according to the values of a scalar field defined on that surface:

```julia
using PlotlyJS

n= 256
θ = LinRange(0, 2π, n) #\theta
ϕ = LinRange(-π/2, π/2, n)
Θ = [t for t in θ, p in ϕ] #\Theta
Φ = [p for t in θ, p in ϕ]
x= cos.(Θ) .* cos.(Φ)
y= sin.(Θ) .* cos.(Φ)
z= sin.(Φ)

J = [j for i in 0:n-1, j in 0:n-1]
ScalarF = xor.(J', J)

pl= Plot(surface(x=x, y=y, z=z, colorbar_len=0.7, surfacecolor=ScalarF), 
         Layout(width=500, height=500))

```

![scalarf](https://global.discourse-cdn.com/julialang/original/3X/5/2/522efb5ba95263829d9e3480899e50f80a2b0ea3.png)

---

<div class="post-metadata">

**Author:** ![Norbert-droid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/norbert-droid/32/20364_2.png) [@Norbert-droid](https://discourse.julialang.org/u/Norbert-droid)\
**Post date:** [August 17, 2021, 10:42pm UTC](https://discourse.julialang.org/t/use-plots-library-to-graph-a-colored-surface/66561/5 "2021-08-17T22:42:14Z")

</div>

The code function directly in Julia. But in jupyter notebook the library PlotlyJS I got the error:

“Unable to load WebIO. Please make sure WebIO works for your Jupyter client. For troubleshooting, please see [the WebIO/IJulia documentation](https://juliagizmos.github.io/WebIO.jl/latest/providers/ijulia/).”

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 18, 2021, 11:02pm UTC](https://discourse.julialang.org/t/use-plots-library-to-graph-a-colored-surface/66561/6 "2021-08-18T23:02:25Z")

</div>

In order to plot a colored surface with Plots.jl pyplot(), give a fourth coordinate meshgrid array `h` to the keyword argument `fill_z`:

```julia
surface(x, y, z; fill_z = h)

```

**Working example with Plots.jl pyplot()**

Preparation:

```julia
using Plots, StaticArrays, Parameters
default(colorbar=false)

rotate(u, v, w) = let c = cosd, s = sind
    SMatrix{3, 3}(
        c(u)*c(v), c(u)*s(v)*s(w)-s(u)*c(w), c(u)*s(v)*c(w)+s(u)*s(w),
        s(u)*c(v), s(u)*s(v)*s(w)+c(u)*c(w), c(u)*s(v)*s(w)-c(u)*s(w),
            -s(v), c(v)*s(w), c(v)*c(w))'
end

"""Assume `surffunc(u, v)` returns `(x, y, z, h)`."""
function plot_surface(u, v, surffunc, param; R = rotate(0, 0, 0), kwargs...)
    xyzh = surffunc.(u', v, Ref(param))
    xyz = (((x, y, z, h),) -> R * SVector(x, y, z)).(xyzh)
    x, y, z = ((a -> a[i]).(xyz) for i in 1:3)
    fill_z = (((x, y, z, h),) -> h).(xyzh)
    surface(x, y, z; fill_z, kwargs...)
end

```

Colored torus:

```julia
pyplot(fmt=:png)

function torusfunc(u, v, param)
    @unpack a, b, d = param
    x = (b + a * cos(u)) * cos(v)
    y = (b + a * cos(u)) * sin(v)
    z = a * sin(u)
    h = d' * SVector(x, y, z)
    x, y, z, h
end

torusparam = (a = 5, b = 10, d = SVector(1, 0, 1))
n = 50
u_torus = v_torus = range(0, 2π; length = n + 1)
plot_surface(u_torus, v_torus, torusfunc, torusparam; size=(500, 400), 
    lims=(-20, 20), color=:CMRmap, R=rotate(30, 30, 30))

```

![torus](https://global.discourse-cdn.com/julialang/original/3X/b/5/b5b0303d0cec04d93b485db3046150678f966182.png)

Animation:

```julia
pyplot(fmt=:png)
@time anim = @animate for t in range(0, 360; length=41)[1:end-1]
    plot_surface(u_torus, v_torus, torusfunc, torusparam; size=(400, 300),
        lims=(-20, 20), color=:CMRmap, R=rotate(30, 30, 30),
        camera=(t, 20), ticks=false)
end
PyPlot.clf()
gif(anim, "torus.gif")

```

![torus](https://global.discourse-cdn.com/julialang/original/3X/b/0/b0de78974b595fa4c0f2be220248803a89c68630.gif)

Colored sphere:

```julia
pyplot(fmt=:png)

function spherefunc(u, v, param)
    @unpack r, d, f = param
    x = r * cos(u) * cos(v)
    y = r * cos(u) * sin(v)
    z = r * sin(u)
    h = f(d' * SVector(x, y, z))
    x, y, z, h
end

sphereparam = (
    r = 20,
    d = SVector(0, 0, 1),
    f = h -> (1 + abs(h - 1)) * sin(h)
)
n = 50
u_sphere = range(-π/2, π/2; length = n + 1)
v_sphere = range(0, 2π; length = 2n + 1)
plot_surface(u_sphere, v_sphere, spherefunc, sphereparam; size=(500, 400), 
    lims=(-20, 20), color=:gist_earth, R=rotate(30, 30, 30),
    camera=(30, 20))

```

![sphere](https://global.discourse-cdn.com/julialang/original/3X/f/3/f333cdb8ecad359917aff8ff6845e92d8c7d4fba.png)

Animation:

```julia
pyplot(fmt=:png)
@time anim = @animate for t in range(0, 360; length=41)[1:end-1]
    plot_surface(u_sphere, v_sphere, spherefunc, sphereparam; size=(400, 300),
        lims=(-20, 20), color=:gist_earth, R=rotate(30, 30, 30),
        camera=(t, 20), ticks=false)
end
PyPlot.clf()
gif(anim, "sphere.gif")

```

![sphere](https://global.discourse-cdn.com/julialang/original/3X/8/3/8330e366a593054cd1cdd4e5f78e218a118ef2eb.gif)

**Interactive plotting with plotly()**

The plotly() backend can be used to display interactive plots in Jupyter notebook.

- [sample Jupyter notebook](https://nbviewer.jupyter.org/github/genkuroki/public/blob/main/0016/3d%20colored%20surface%20plots.ipynb)

 ![2021-08-19 (1)](https://global.discourse-cdn.com/julialang/original/3X/6/8/680c276836edf4b51428e05c5c8a6763d4082395.png)

---

<div class="post-metadata">

**Author:** ![HerAdri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heradri/32/5816_2.png) [@HerAdri](https://discourse.julialang.org/u/HerAdri)\
**Post date:** [August 19, 2021, 11:18am UTC](https://discourse.julialang.org/t/use-plots-library-to-graph-a-colored-surface/66561/7 "2021-08-19T11:18:42Z")

</div>

> [@genkuroki](#):
>
> Colored torus:
> 
> ```julia
> pyplot(fmt=:png)
> 
> function torusfunc(u, v, param)
> @unpack a, b, d = param
> x = (b + a * cos(u)) * cos(v)
> y = (b + a * cos(u)) * sin(v)
> z = a * sin(u)
> h = d' * SVector(x, y, z)
> x, y, z, h
> end
> 
> torusparam = (a = 5, b = 10, d = SVector(1, 0, 1))
> n = 50
> u_torus = v_torus = range(0, 2π; length = n + 1)
> plot_surface(u_torus, v_torus, torusfunc, torusparam; size=(500, 400), 
> lims=(-20, 20), color=:CMRmap, R=rotate(30, 30, 30))
> 
> ```

```julia
julia> plot_surface(u_torus, v_torus, torusfunc, torusparam; size=(500, 400),
           lims=(-20, 20), color=:CMRmap, R=rotate(30, 30, 30))
sys:1: MatplotlibDeprecationWarning: Passing the fontdict parameter of _set_ticklabels() positionally is deprecated since Matplotlib 3.3; the parameter will become keyword-only two minor releases later.
sys:1: UserWarning: FixedFormatter should only be used together with FixedLocator
Error showing value of type Plots.Plot{Plots.PyPlotBackend}:
ERROR: MethodError: no method matching getproperty(::Char, ::String)
Closest candidates are:
  getproperty(::PyPlot.ColorMap, ::AbstractString) at C:\Users\Hermesr\.julia\packages\PyPlot\XHEG0\src\colormaps.jl:26
  getproperty(::PyPlot.LazyPyModule, ::AbstractString) at C:\Users\Hermesr\.julia\packages\PyPlot\XHEG0\src\plot3d.jl:16
  getproperty(::PyCall.PyObject, ::AbstractString) at C:\Users\Hermesr\.julia\packages\PyCall\BD546\src\PyCall.jl:311
  ...
Stacktrace:
  [1] py_set_axis_colors(sp::Plots.Subplot{Plots.PyPlotBackend}, ax::PyCall.PyObject, a::Plots.Axis)
    @ Plots C:\Users\Hermesr\.julia\packages\Plots\hyS17\src\backends\pyplot.jl:815
  [2] _before_layout_calcs(plt::Plots.Plot{Plots.PyPlotBackend})
    @ Plots C:\Users\Hermesr\.julia\packages\Plots\hyS17\src\backends\pyplot.jl:1100
  [3] prepare_output(plt::Plots.Plot{Plots.PyPlotBackend})
    @ Plots C:\Users\Hermesr\.julia\packages\Plots\hyS17\src\plot.jl:177
  [4] display(d::VSCodeServer.InlineDisplay, m::MIME{Symbol("image/svg+xml")}, x::Plots.Plot{Plots.PyPlotBackend})
    @ VSCodeServer c:\Users\Hermesr\.vscode\extensions\julialang.language-julia-1.3.30\scripts\packages\VSCodeServer\src\display.jl:0
  [5] display(d::VSCodeServer.InlineDisplay, mime::String, x::Any)
    @ Base.Multimedia .\multimedia.jl:216
  [6] display(d::VSCodeServer.InlineDisplay, x::Plots.Plot{Plots.PyPlotBackend})
    @ VSCodeServer c:\Users\Hermesr\.vscode\extensions\julialang.language-julia-1.3.30\scripts\packages\VSCodeServer\src\display.jl:109
  [7] display(x::Any)
    @ Base.Multimedia .\multimedia.jl:328
  [8] #invokelatest#2
    @ .\essentials.jl:708 [inlined]
  [9] invokelatest
    @ .\essentials.jl:706 [inlined]
 [10] print_response(errio::IO, response::Any, show_value::Bool, have_color::Bool, specialdisplay::Union{Nothing, AbstractDisplay})
    @ REPL C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\REPL\src\REPL.jl:247
 [11] (::REPL.var"#40#41"{REPL.LineEditREPL, Pair{Any, Bool}, Bool, Bool})(io::Any)
    @ REPL C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\REPL\src\REPL.jl:231
 [12] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\REPL\src\REPL.jl:462
 [13] print_response(repl::REPL.AbstractREPL, response::Any, show_value::Bool, have_color::Bool)
    @ REPL C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\REPL\src\REPL.jl:229
 [14] (::REPL.var"#do_respond#61"{Bool, Bool, REPL.var"#72#82"{REPL.LineEditREPL, REPL.REPLHistoryProvider}, REPL.LineEditREPL, REPL.LineEdit.Prompt})(s::REPL.LineEdit.MIState, buf::Any, ok::Bool)
    @ REPL C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\REPL\src\REPL.jl:798
 [15] #invokelatest#2
    @ .\essentials.jl:708 [inlined]
 [16] invokelatest
    @ .\essentials.jl:706 [inlined]
 [17] run_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
    @ REPL.LineEdit C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\REPL\src\LineEdit.jl:2441
 [18] run_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)
    @ REPL C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\REPL\src\REPL.jl:1126
 [19] (::REPL.var"#44#49"{REPL.LineEditREPL, REPL.REPLBackendRef})()
    @ REPL .\task.jl:411

```

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 19, 2021, 12:15pm UTC](https://discourse.julialang.org/t/use-plots-library-to-graph-a-colored-surface/66561/8 "2021-08-19T12:15:16Z")

</div>

My environment is Windows 10 with

```julia
using Pkg
println("Julia v", VERSION)
Pkg.status("IJulia")
Pkg.status("PyCall"; mode = PKGMODE_MANIFEST)
Pkg.status("Plots")
Pkg.status("PyPlot")
Pkg.status("Plotly")
Pkg.status("StaticArrays")
Pkg.status("Parameters")

```

↓

```julia
Julia v1.6.2
      Status `D:\.julia\environments\v1.6\Project.toml`
  [7073ff75] IJulia v1.23.2
      Status `D:\.julia\environments\v1.6\Manifest.toml`
  [438e738f] PyCall v1.92.3
      Status `D:\.julia\environments\v1.6\Project.toml`
  [91a5bcdd] Plots v1.20.1
      Status `D:\.julia\environments\v1.6\Project.toml`
  [d330b81b] PyPlot v2.9.0
      Status `D:\.julia\environments\v1.6\Project.toml`
  [58dd65bb] Plotly v0.4.0
      Status `D:\.julia\environments\v1.6\Project.toml`
  [90137ffa] StaticArrays v1.2.12
      Status `D:\.julia\environments\v1.6\Project.toml`
  [d96e819e] Parameters v0.12.2

```

I use Plots.jl v1.20.1 installed in the directory

```julia
D:\.julia\packages\Plots\HcxwM

```

But your Plots.jl directory is

```julia
C:\Users\Hermesr\.julia\packages\Plots\hyS17

```

Your hyS17 is not equal to my HcxwM.

> [@HerAdri](#):
>
> ```julia
> Stacktrace:
> [1] py_set_axis_colors(sp::Plots.Subplot{Plots.PyPlotBackend}, ax::PyCall.PyObject, a::Plots.Axis)
> @ Plots C:\Users\Hermesr\.julia\packages\Plots\hyS17\src\backends\pyplot.jl:815
> 
> ```

Probably you are using an older version of Plots.jl due to conflicts with other packages.

First, upgrade installed packages by

```julia
pkg> up

```

and check if Plots.jl is upgraded to version ≥ v1.20.1 or not.

If not, restart Julia in an empty directory and run

```julia
pkg> activate .
pkg> add Plots
pkg> status

```

Confirm the version of Plots.jl is ≥ v1.20.1. Then try to run my code.

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 19, 2021, 1:31pm UTC](https://discourse.julialang.org/t/use-plots-library-to-graph-a-colored-surface/66561/9 "2021-08-19T13:31:07Z")

</div>

I have created [the Pluto notebook version](https://github.com/genkuroki/public/blob/main/0016/Pluto%20-%203d%20colored%20surface%20plots.jl) of [the sample Jupyter notebook above](https://nbviewer.jupyter.org/github/genkuroki/public/blob/main/0016/3d%20colored%20surface%20plots.ipynb).

If you have trouble resolving package dependency issues, you may try the Pluto notebook version. [Pluto](https://github.com/fonsp/Pluto.jl) has a feature automatically resolving package dependencies.

Please copy-and-paste the GitHub URL of Pluto notebook into the browser tab opened by Pluto.

![2021-08-19 (3)](https://global.discourse-cdn.com/julialang/original/3X/f/b/fb9a017ba01ac1873e927e4cab45077ea8cc96bb.png)

 ![2021-08-19 (8)](https://global.discourse-cdn.com/julialang/original/3X/2/5/25bdacc071b2e3bb788151f1149da0da0c32a10b.png)

The URL that should be pasted is

```julia
https://github.com/genkuroki/public/blob/main/0016/Pluto%20-%203d%20colored%20surface%20plots.jl

```
