# How to create a Surface plot in Plot.jl from \`\`\`solve()\`\`\` data

**URL:** <https://discourse.julialang.org/t/how-to-create-a-surface-plot-in-plot-jl-from-solve-data/96684>\
**Category:** New to Julia\
**Tags:** question, plotting\
**Created:** [March 27, 2023, 10:07pm UTC](https://discourse.julialang.org/t/how-to-create-a-surface-plot-in-plot-jl-from-solve-data/96684 "2023-03-27T22:07:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KZ-Spectra](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@KZ-Spectra](https://discourse.julialang.org/u/KZ-Spectra)\
**Post date:** [March 27, 2023, 10:07pm UTC](https://discourse.julialang.org/t/how-to-create-a-surface-plot-in-plot-jl-from-solve-data/96684/1 "2023-03-27T22:07:40Z")

</div>

**MWE:**

```plaintext
N₁=5
γ=-1 #nonlinear term strength parameter

g₁(u,p,t) = 1*im*(p[1]*Tridiagonal(fill(1,N₁), fill(-2,N₁+1), fill(1,N₁))*u + γ*u.^3)
vals=0.3*ones(N₁+1)
u0 =map(Complex,vals)

tspan = (0.0,1.0)
prob = ODEProblem(g₁,u0,tspan, [0.6, 1])
sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)

```

I want to create a surface plot of the absolute value squared data in `sol` similar to what is show here:

 ![dZK33mk](https://global.discourse-cdn.com/julialang/original/3X/a/b/ab0e7ff7dc3bfb1d48110acff97dbe4b48aad77f.png)

**Attempt:** I want to use the `Plots.jl` package and utilize the `surface()` function. The time data in `sol` can be accessed as `sol.t` and the spatial data can be accessed as `sol.u`. For example, at `sol.t[1]` the corresponding spatial data is a 6-component vector `sol.u[2]` given by `6-element Vector{ComplexF64}: 0.2999679677368571 - 0.0030866750511122525im 0.30001119699789686 - 0.000402829297042341im 0.2999991898889756 - 0.00040261260929592426im 0.2999991898889756 - 0.00040261260929592426im 0.30001119699789686 - 0.000402829297042341im 0.2999679677368571 - 0.0030866750511122525im`

The components of the vector are the spatial data at the first, second, third, fourth, fifth, and sixth grid points of the discretized space. I know via broadcasting I can obtain the absolute valued squared data by `abs.sol`

I tried `surface(range(-5,5), sol.t, abs.(sol))`, but this resulted in an error.

---

<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:** [March 28, 2023, 8:24am UTC](https://discourse.julialang.org/t/how-to-create-a-surface-plot-in-plot-jl-from-solve-data/96684/2 "2023-03-28T08:24:48Z")

</div>

A surface is either defined explicitly as z=f(x,y), (x,y)\in D\subset\mathbb{R}^2 or parametric: (u,v)\to(x(u,v), y(u,v), z(u,v)), with (u,v) in a a rectangular region or a disk, etc.  
Your sol has length(sol.t)= length(sol.u)=19, and to each `sol.t[j]`, j=1:19,  
corresponds a vector sol.u[j] of length 6.

If you associate to each `sol.t[j]` the absolut values,  
`abs.(sol.u[j])=(abs(sol.u[j][1]), abs(sol.u[j][2]), ... abs(sol.u[j][6])`  
you get a map s\in \mathbb{R}\to\mathbb{R}^6, and it doesn’t define a surface, but a curve in a 6-dimensional space.

---

<div class="post-metadata">

**Author:** ![KZ-Spectra](https://avatars.discourse-cdn.com/v4/letter/k/2acd7d/32.png) [@KZ-Spectra](https://discourse.julialang.org/u/KZ-Spectra)\
**Post date:** [March 28, 2023, 7:19pm UTC](https://discourse.julialang.org/t/how-to-create-a-surface-plot-in-plot-jl-from-solve-data/96684/3 "2023-03-28T19:19:35Z")

</div>

The spatial data, `(abs(sol.u[j][1]), abs(sol.u[j][2]), ... abs(sol.u[j][6])` are slices of the surface at time `sol.u[j]`.

---

<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:** [March 28, 2023, 8:35pm UTC](https://discourse.julialang.org/t/how-to-create-a-surface-plot-in-plot-jl-from-solve-data/96684/4 "2023-03-28T20:35:34Z")

</div>

Well, if you take:

```julia
using Plots
plotlyjs()
 x=sol.t
 y = range(-5, 5, length=6)
 z= [abs(sol.u[i][j]) for j=1:6, i=1:19]
 surface(x, y, z)

```

you’ll get the surface.  
Since you didn’t give any information on the six y-values for each sol.t[i], I defined them as being equally distanced.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [March 29, 2023, 1:44am UTC](https://discourse.julialang.org/t/how-to-create-a-surface-plot-in-plot-jl-from-solve-data/96684/5 "2023-03-29T01:44:11Z")

</div>

Just in case you weren’t aware of the existing integrations with `Plots.jl`: [Plot Functions](https://docs.sciml.ai/DiffEqDocs/stable/basics/plot/#plot)
