# How to plot solution from ODEProblem

**URL:** <https://discourse.julialang.org/t/how-to-plot-solution-from-odeproblem/97148>\
**Category:** New to Julia\
**Created:** [April 6, 2023, 4:01am UTC](https://discourse.julialang.org/t/how-to-plot-solution-from-odeproblem/97148 "2023-04-06T04:01:05Z")\
**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:** [April 6, 2023, 4:01am UTC](https://discourse.julialang.org/t/how-to-plot-solution-from-odeproblem/97148/1 "2023-04-06T04:01:05Z")

</div>

I am trying to plot the solution vector `sol` from the solver of an `ODEProblem` which is a 1432 element vector of 31x1 vectors. To do this, I attempted

```julia
plotlyjs()
 y= sol.t 
 z= [abs(sol.u[i][j])^2 for j=1:N₁, i=1:size(sol)[2]]
 surface(x, y, z)

```

where `N₁=31`

However, this never finishes compiling. What is the proper way of plotting my solution? Below is the full MWE.

```julia
using DifferentialEquations, LinearAlgebra, Plots, SparseArrays
N₁=31 # Number of waveguides
γ=-1 # Nonlinear term strength parameter
h=0.6 # Grid spacing 
centerGrid = (N₁-1)/2;

x = -centerGrid:centerGrid;

# Coefficient matrix of second-order centered-difference operator (δ²u)ₙ
M = spdiagm(-1 => fill(1,N₁-1), 0 => fill(-2,N₁), 1 => fill(1,N₁-1))
M[N₁,1] = 1; # Periodic boundary conditions
M[1,N₁] = 1;

# RHS of DNLS. The solution vector u is a N₁x1 complex vector
g₁(u,p,t) = 1*im*(p[1]*Tridiagonal(fill(1,N₁-1), fill(-2,N₁), fill(1,N₁-1))*u + γ*u.^3)

u0 = sech.(x);
u0 = map(Complex,u0)
tspan = (0.0,100)

prob = ODEProblem(g₁,u0,tspan, [h])

sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)

plotlyjs()
 y= sol.t 
 z= [abs(sol.u[i][j])^2 for j=1:N₁, i=1:size(sol)[2]]
 surface(x, y, z)

```

---

<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:** [April 6, 2023, 7:35am UTC](https://discourse.julialang.org/t/how-to-plot-solution-from-odeproblem/97148/2 "2023-04-06T07:35:15Z")

</div>

Your z is a 31x1432 Matrix(Float64) . Hence to get the surface plotted, y must be a vector of length 31, and x of length 1432, but your length(x) and length(y) are permuted.

```julia
surface(sol.t, x, z)

```

is the right call of surface.

---

<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:** [April 7, 2023, 3:40am UTC](https://discourse.julialang.org/t/how-to-plot-solution-from-odeproblem/97148/3 "2023-04-07T03:40:50Z")

</div>

> [@empet](#):
>
> `surface(sol.t, x, z)`

This does not compile for me in a notebook environment. In fact, I cannot interrupt the compile and must close the IDE (VScode).

Strange… without `plotlyjs()` it compiles. but I want `plotlyjs()`… Why does it not compile with this backend?

---

<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:** [April 7, 2023, 7:09am UTC](https://discourse.julialang.org/t/how-to-plot-solution-from-odeproblem/97148/4 "2023-04-07T07:09:07Z")

</div>

I used plotlyjs(), and the colormap matter:  
`surface(sol.t, x, z, color=:matter)`

 ![surf-DEsol](https://global.discourse-cdn.com/julialang/original/3X/d/a/da691b1286f8eabb5d1f912bb1c2a86207b023a9.png)

---

<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:** [April 7, 2023, 5:56pm UTC](https://discourse.julialang.org/t/how-to-plot-solution-from-odeproblem/97148/5 "2023-04-07T17:56:35Z")

</div>

The issue seems to be using a notebook environment. Using `plotlyjs()` as you wrote works as intended.
