# Plot sol of differentialequation

**URL:** https://discourse.julialang.org/t/plot-sol-of-differentialequation/108768
**Category:** Visualization
**Tags:** plots, differentialequation
**Created:** [January 13, 2024, 12:53pm UTC](https://discourse.julialang.org/t/plot-sol-of-differentialequation/108768 "2024-01-13T12:53:27Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Stephen](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Stephen](https://discourse.julialang.org/u/Stephen)
#### Post date: [January 13, 2024, 12:53pm UTC](https://discourse.julialang.org/t/plot-sol-of-differentialequation/108768/1 "2024-01-13T12:53:27Z")

</div>

```julia
using Plots, DifferentialEquations
function projectile_motion(v₀, θ)
    g = 9.8067 # acceleration due to gravity (m/s^2)
    t_final = 2 * v₀ * sind(θ) / g # total flight time
    
    # Define the system of second-order ODEs
    function projectile!(du, u, p, t)
        x, y = u
        ddu = [0 -g]
    end

    # Initial conditions
    (u₀, du₀) = [[0.0 0.0], [v₀ * cosd(θ) v₀ * sind(θ)]]
    
    # Time span
    t_span = [0.0, 20]
    
    # Solve the ODE
    prob = SecondOrderODEProblem(projectile!, du₀, u₀, t_span)
    sol = solve(prob, Tsit5(), dt=0.001)
    
    return sol # return the value of du u
end

# Initial velocity and launch angle
v₀ = 100.0 # initial velocity in m/s
θ = 45.0 # launch angle in degrees

# Simulate projectile motion
sol = projectile_motion(v₀, θ)

```

the type of sol.u is a Vector

Plot the results gives：

```julia
plot(sol, title = "Projectile Motion", xaxis = "Time in Seconds",
yaxis = "Velocity (m/s) | Position(m)",labels = ["x Velocity" "y Velocity" "x Position" "y Position"], widen = true, xlims = (0.0, 20.0), ylims = (-500.0, 1500.0))

```

 ![plot_10](https://global.discourse-cdn.com/julialang/original/3X/e/c/ec45c1dd2ddae5f347db65053ab154fdda1c2466.png)

using ` twinx()` how to display velocity and position separately？ what syntax can change the structure or type of sol.u？  
by the way, I want labels box off，what syntax should be used？

---

<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: [January 13, 2024, 2:32pm UTC](https://discourse.julialang.org/t/plot-sol-of-differentialequation/108768/2 "2024-01-13T14:32:30Z")

</div>

Try this [edited code]:

```julia
using Plots, Measures
plot(sol, idxs=1, c=:blue, label=false, xlabel="", ylabel="Velocity [m/s]", margin=10mm)
plot!(sol, idxs=2, c=:blues, label=false, xlabel="", yguidefontcolor=:blue)
p2 = twinx()
plot!(p2, sol, idxs=3, c=:red, label=false, xlabel="", ylabel="Position [m]", yguidefontcolor=:red)
plot!(p2, sol, idxs=4, c=:reds, label=false, xlabel="\nTime [s]")

```

---

<div class="post-metadata">

### Author: ![Stephen](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Stephen](https://discourse.julialang.org/u/Stephen)
#### Post date: [January 14, 2024, 1:33am UTC](https://discourse.julialang.org/t/plot-sol-of-differentialequation/108768/3 "2024-01-14T01:33:33Z")

</div>

![plot_10](https://global.discourse-cdn.com/julialang/original/3X/6/9/696c90a04633b0aca9ab4eed45cea46828dc8984.png)  
it works ok, yet not perfect, thanks anyway

---

<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: [January 14, 2024, 4:58am UTC](https://discourse.julialang.org/t/plot-sol-of-differentialequation/108768/4 "2024-01-14T04:58:34Z")

</div>

Please check the revised code above.

It produces this plot:

 ![Plots_DiffEq_twinx_axes](https://global.discourse-cdn.com/julialang/original/3X/3/2/32e8c2c80b91910d63513a687d6a00c61108c997.png)
