# Plotting DifferentialEquations.jl solutions, re-indexing

**URL:** <https://discourse.julialang.org/t/plotting-differentialequations-jl-solutions-re-indexing/108106>\
**Category:** Visualization\
**Created:** [December 27, 2023, 5:00pm UTC](https://discourse.julialang.org/t/plotting-differentialequations-jl-solutions-re-indexing/108106 "2023-12-27T17:00:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aoifehealy](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@aoifehealy](https://discourse.julialang.org/u/aoifehealy)\
**Post date:** [December 27, 2023, 5:00pm UTC](https://discourse.julialang.org/t/plotting-differentialequations-jl-solutions-re-indexing/108106/1 "2023-12-27T17:00:22Z")

</div>

Hi there, I am solving a system of differential equations using Differential equations.jl, and have the following result:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/9/999c65d7588670baa51e43b79629c7854065c51b.png)  
I would like to only plot the solution when it has stabilised (from 5 seconds onwards). I have been using `plot(sol, xlims=(5.0,10.0))`, which yields  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/6/86c608f6f3ac7e4777996c41e420ada46c8d8907.png)  
but I am wondering if there is a way to re-index so that what used to be t=5 is now t=0 so that the x axis starts at zero instead of 5.

Thanks!

---

<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:** [December 27, 2023, 7:30pm UTC](https://discourse.julialang.org/t/plotting-differentialequations-jl-solutions-re-indexing/108106/2 "2023-12-27T19:30:20Z")

</div>

```julia
n=12 #set n such that sol.t[n]=5
plot(sol.t[n:end], sol.u[n:end], xlims=(sol.t[1], sol.t[end]))

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 27, 2023, 7:33pm UTC](https://discourse.julialang.org/t/plotting-differentialequations-jl-solutions-re-indexing/108106/3 "2023-12-27T19:33:40Z")

</div>

You can pass a `tspan` to the plot. `plot(sol,tspan=(5,10))`

---

<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:** [December 27, 2023, 7:37pm UTC](https://discourse.julialang.org/t/plotting-differentialequations-jl-solutions-re-indexing/108106/4 "2023-12-27T19:37:32Z")

</div>

Is OP just looking for a change of variable:  
`t' = t .- 5` ?  
Then, it should suffice to plot:

```julia
plot(sol.t .- 5, sol.u, xlims=(0, 5))

```

?

---

<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:** [December 27, 2023, 8:30pm UTC](https://discourse.julialang.org/t/plotting-differentialequations-jl-solutions-re-indexing/108106/5 "2023-12-27T20:30:59Z")

</div>

It’s not quite clear what he wants. I supposed that he intend to illustrate that the solution stabilizes starting from a time t’\>0, and wanted to plot that solution above the integration time of the general solution, i.e. `xlims=(sol.t[1], sol.t[end])`. But I’m not sure…

---

<div class="post-metadata">

**Author:** ![aoifehealy](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@aoifehealy](https://discourse.julialang.org/u/aoifehealy)\
**Post date:** [December 27, 2023, 9:21pm UTC](https://discourse.julialang.org/t/plotting-differentialequations-jl-solutions-re-indexing/108106/6 "2023-12-27T21:21:59Z")

</div>

\*she

Thank you for your replies, I used the following workaround:

```julia
n = 1
for t in sol.t
    if t>10
        break
    end
    n+=1
end

x= sol.t[n:end] .- sol.t[n]
y = sol[systemic_circulation.source.Δp][n:end]

plot(x,y)

```

---

<div class="post-metadata">

**Author:** ![cmdenis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmdenis/32/211517_2.png) [@cmdenis](https://discourse.julialang.org/u/cmdenis)\
**Post date:** [June 14, 2024, 6:35pm UTC](https://discourse.julialang.org/t/plotting-differentialequations-jl-solutions-re-indexing/108106/7 "2024-06-14T18:35:14Z")

</div>

The question seems to be resolved, but just in case someone is in a similar situation and is looking for alternatives:

To deal with a transient time in some simulation, I usually use something like this:

```julia
# Define system of equation 
function f(du, u, p, t)
    du[1] = -u[1]
end

# Parameter for system (in this case there are none)
p = []

# Initial conditions
u0 = [1]

# Time points to integrate
times = 0:0.01:8

# Transient time
transient = 1

# Define ODE problem
prob = ODEProblem(
    f, # system to integrate
    u0, # initial conditions
    (times[1], times[end] + transient), # time bounds to integrate in
    p # parameters for the system
)

# Solve problem
sol = solve(
    prob, # problem to solve for
    saveat = times .+ transient # times to get an answer for, but shifted by the transient time
    )

# Plot trajectories
plot(sol)

```

Someone, please feel free to correct me if this is an unnatural or inefficient way of doing things!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 14, 2024, 6:51pm UTC](https://discourse.julialang.org/t/plotting-differentialequations-jl-solutions-re-indexing/108106/8 "2024-06-14T18:51:08Z")

</div>

That is fine
