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

**URL:** <https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902>\
**Category:** Visualization\
**Created:** [December 5, 2019, 12:26pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902 "2019-12-05T12:26:40Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [December 5, 2019, 12:26pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/1 "2019-12-05T12:26:40Z")

</div>

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.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/8/5899d8d51f2bd509c1d43b8d0d9b1f67ad972c21.png)

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

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [December 5, 2019, 12:34pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/2 "2019-12-05T12:34:29Z")

</div>

`vcat` both time series, maybe?

---

<div class="post-metadata">

**Author:** ![mariok90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariok90/32/7076_2.png) [@mariok90](https://discourse.julialang.org/u/mariok90)\
**Post date:** [December 5, 2019, 12:35pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/3 "2019-12-05T12:35:18Z")

</div>

Have you tried:

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

```

?

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [December 5, 2019, 1:29pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/4 "2019-12-05T13:29:26Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [December 5, 2019, 1:36pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/5 "2019-12-05T13:36:00Z")

</div>

> [@HelgavonLichtenstein](#):
>
> @mariok90 The graphs just plot on top of each other, not next to each other.

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

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [December 5, 2019, 1:39pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/6 "2019-12-05T13:39:24Z")

</div>

```julia
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])

```

Colour added for emphasis

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/0/109efc52ef8b70ced53c59a213e46e9d58fd7bda.png)

---

<div class="post-metadata">

**Author:** ![mariok90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariok90/32/7076_2.png) [@mariok90](https://discourse.julialang.org/u/mariok90)\
**Post date:** [December 5, 2019, 2:11pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/7 "2019-12-05T14:11:08Z")

</div>

I am not sure what you are looking for?

Is it that:

```julia
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="")

```

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [December 5, 2019, 2:20pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/8 "2019-12-05T14:20:18Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [December 5, 2019, 2:32pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/9 "2019-12-05T14:32:05Z")

</div>

@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?

```julia
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]`.

---

<div class="post-metadata">

**Author:** ![mariok90](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariok90/32/7076_2.png) [@mariok90](https://discourse.julialang.org/u/mariok90)\
**Post date:** [December 5, 2019, 2:46pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/10 "2019-12-05T14:46:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [December 5, 2019, 2:48pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/11 "2019-12-05T14:48:22Z")

</div>

More like this:

```julia
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.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [December 5, 2019, 2:58pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/12 "2019-12-05T14:58:27Z")

</div>

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 .

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [December 5, 2019, 3:25pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/13 "2019-12-05T15:25:29Z")

</div>

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

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

@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.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [December 5, 2019, 3:32pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/14 "2019-12-05T15:32:46Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [December 5, 2019, 3:49pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/15 "2019-12-05T15:49:20Z")

</div>

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

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

```

```julia
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.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [December 5, 2019, 3:53pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/16 "2019-12-05T15:53:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [December 5, 2019, 4:16pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/17 "2019-12-05T16:16:31Z")

</div>

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:

```julia
using DifferentialEquations, Plots
function lorenz!(du,u,p,t)
 du[1] = 10.0*(u[2]-u[1])
 du[2] = u[1]*(28.0-u[3]) - u[2]
 du[3] = u[1]*u[2] - (8/3)*u[3]
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)

```

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [December 5, 2019, 4:33pm UTC](https://discourse.julialang.org/t/how-to-splice-two-time-series-into-one-in-a-plot/31902/18 "2019-12-05T16:33:44Z")

</div>

IT WORKS!!! ❤❤😃

Thank you so much! I will finish my poster in time for the conference 🙏
