# How to splice two time series into one in a plot?

Hello, I have two differential equation time series (`sol` and `sol2`).
`sol[end]` is used as the starting values for `sol2`. I would like to plot these two on the same plot, if possible, like so.

I can graph them separately, but I don’t know how to put them onto one plot.

``````plot(sol, tspan=(0.0,5000.0), title = "Pristine", xlabel = "Time" ,ylabel = "Biomass", lw=0.3)
plot(sol2, tspan=(0.0,5000.0), title = "With Harvest", xlabel = "Time" ,ylabel = "Biomass", lw=0.3)
``````

`vcat` both time series, maybe?

Have you tried:

``````plot(sol, ...)
plot!(sol2, ...)
``````

?

1 Like

@mariok90 The graphs just plot on top of each other, not next to each other.

@heliosdrm I am getting an error with `vcat` saying the number of columns are not matching. I don’t understand why as both `sol` and `sol2` run for the same amount of time steps.

That should have been the correct solution, though - can you post an example?

``````using DifferentialEquations, Plots

plot(sol, tspan=(0.0,tmax), title = "Pristine", xlabel = "Time" ,ylabel = "Biomass", lw=0.3, linecolor=[:red])
plot!(sol2, tspan=(0.0,tmax), title = "With Harvest", xlabel = "Time" ,ylabel = "Biomass", legend = false, lw=0.3, linecolor=[:blue])
``````

I am not sure what you are looking for?

Is it that:

``````using Plots

# some random data
sol = rand(4,3)
sol2 =rand(5,3)
sol2[1,:] = sol[end,:]

len1 = size(sol, 1)
len2 = size(sol2, 1)
colors = [:blue :red :yellow]
sol_max = maximum(vcat(sol, sol2))*1.1

plot(1:len1, sol,
label=["" "" ""],
color=colors,
annotations=(len1/2,sol_max,text("Pristine",:center)),
ylims=(0,sol_max))

plot!(range(len1, length=len2), sol2,
color=colors,
annotations=(len1 + len2/2,sol_max,text("With Harvest",:center)),
leg=:bottomright)

vline!([len1], color=:black, label="")
``````

The issue is that both solutions have times starting from 0. You have to shift the second to the duration of the first. Like

``````sol2.t .+= sol.t[end]
``````

@mariok90
Yes, that is what I am looking for! However, I don’t understand the purpose of the yellow line - where did it come from?

@BeastyBlacksmith Like this?

``````plot!(sol2, tspan=(sol2.t .+= sol.t[end]), title = "With Harvest", xlabel = "Time" ,ylabel = "Biomass", legend = false, lw=0.3, linecolor=[:blue])
``````

If so it causes an error `BoundsError: attempt to access 43-element Array{Float64,1} at index [1:91]`.

Your opening post contained an example with several lines. So I just generated some random data with an arbitrary amount of lines.

More like this:

``````using DifferentialEquations, Plots
sol2.t .+= sol.t[end]
plot(sol, title = "Pristine  With Harvest", xlabel = "Time" ,ylabel = "Biomass", lw=0.3, linecolor=[:red])
plot!(sol2, legend = false, lw=0.3, linecolor=[:blue])
``````

You will somehow need to adjust the spacing of the titles manually, since you can only have one title in Plots at the moment.

Now it creates two separate graphs (?!), though the time step (on the x-axis) does continue from where `sol` ended.

@mariok90 Thank you for your reply and example. I will play around with it .

Sorry @mariok90, that’s actually not what I am looking for. `sol[end]` does become the beginning of `sol2`, but `sol` does not continue.

What I need is to glue two graphs together

@BeastyBlacksmith upon closer look, I see the time span for the second graph starts at 2000 and not 1000, so perhaps that is why it is creating two separate graphs.

Edit: It’s been a long day. Sorry for my carelessness in responses.

That could have happend if you executed `sol2.t .+= sol.t[end]` twice without resetting `sol2`, however a better idea would be to fix the timespan in your problem definition. Something like

``````prob2 = ODEProblem(f, sol.u[end], (sol.t[end], 2sol.t[end]))
``````

Either way it shouldn’t produce 2 plots, but without seeing the code its hard to tell what went wrong.

Thank you for your reply. The only difference I see between `sol` and `sol2` is:

``````sol
ODECompositeSolution with uType Array{Float64,1} and tType Float64
u →
Vector{Array{Float64,1}} with 152 elements
``````
``````sol2

ODECompositeSolution with uType Array{Float64,1} and tType Float64
u →
Vector{Array{Float64,1}} with 93 elements
``````

I don’t know how there is a different amount of elements between the two. They both run the same code.

can you copy/paste the code that produces these solutions or preferrably a similiar but shorter MWE?

So, the DiffEq recipe sets the xlims to the solution range, so you have to specify the larger range explicitly in the second plot call.
The following works as expected:

``````using DifferentialEquations, Plots
function lorenz!(du,u,p,t)
du = 10.0*(u-u)
du = u*(28.0-u) - u
du = u*u - (8/3)*u
end
u0 = [1.0;0.0;0.0]
tspan = (0.0,100.0)
prob = ODEProblem(lorenz!,u0,tspan)
sol = solve(prob)
prob2 = ODEProblem(lorenz!, sol.u[end], (sol.t[end], 2sol.t[end]))
sol2 = solve(prob2)
##
pl = plot(sol, linecolor = :red)
plot!(pl, sol2, xlims=(0,200), linecolor = :blue)
``````

IT WORKS!!!   Thank you so much! I will finish my poster in time for the conference 1 Like