# How to animate multiple stochastic processes with GLMakie

**URL:** <https://discourse.julialang.org/t/how-to-animate-multiple-stochastic-processes-with-glmakie/116718>\
**Category:** General Usage\
**Tags:** question, plotting, glmakie\
**Created:** [July 7, 2024, 12:40pm UTC](https://discourse.julialang.org/t/how-to-animate-multiple-stochastic-processes-with-glmakie/116718 "2024-07-07T12:40:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [July 7, 2024, 12:40pm UTC](https://discourse.julialang.org/t/how-to-animate-multiple-stochastic-processes-with-glmakie/116718/1 "2024-07-07T12:40:43Z")

</div>

I would like to animate a stochastic process with GLMakie. I have two related questions. First, in the code below, how do I fix the conversion error? Second, how can I programatically generate lines based on the number of columns in the matrix `evidence`? The example below has two columns, but there could be fewer or more columns.

Thank you.

**Code**

```julia
using GLMakie
using SequentialSamplingModels 

# simulate the decision process
model = RDM()
# evidence is a sample by choice matrix
times, evidence = simulate(model)

idx = Observable(1)

# setup plot accumulator for choice 1
ys_1 = @lift(evidence[$idx,1])
# setup plot accumulator for choice 2
ys_2 = @lift(evidence[$idx,2])

fig = lines(times, ys_1, color = :blue, linewidth = 4,
    axis = (title = @lift("t = $(round(times[$idx], digits = 1))"),))
lines!(times, ys_2, color = :red, linewidth = 4)

indices = 1:length(times)
record(fig, "time_animation.mp4", indices;
        framerate = length(times)) do i
    idx[] = i
end

```

**Error**

> **Summary**
>
> ```julia
> ERROR: ArgumentError: Conversion failed for Lines (With conversion trait PointBased()) with args: Tuple{Vector{Float64}, Float64} .
> Lines requires to convert to argument types Tuple{AbstractVector{<:Union{Point2, Point3}}}, which convert_arguments didn't succeed in.
> To fix this overload convert_arguments(P, args...) for Lines or PointBased() and return an object of type Tuple{AbstractVector{<:Union{Point2, Point3}}}.`
> 
> Stacktrace:
> [1] conversion_pipeline(P::Type{…}, used_attrs::Tuple{}, args::Tuple{…}, args_obs::Tuple{…}, user_attributes::Dict{…}, deregister::Vector{…}, recursion::Int64)
> @ Makie ~/.julia/packages/Makie/GtFuI/src/interfaces.jl:240
> [2] conversion_pipeline(P::Type{…}, used_attrs::Tuple{}, args::Tuple{…}, args_obs::Tuple{…}, user_attributes::Dict{…}, deregister::Vector{…}, recursion::Int64)
> @ Makie ~/.julia/packages/Makie/GtFuI/src/interfaces.jl:233
> [3] conversion_pipeline(P::Type{…}, used_attrs::Tuple{}, args::Tuple{…}, args_obs::Tuple{…}, user_attributes::Dict{…}, deregister::Vector{…})
> @ Makie ~/.julia/packages/Makie/GtFuI/src/interfaces.jl:213
> [4] (Lines)(user_args::Tuple{Vector{Float64}, Observable{Float64}}, user_attributes::Dict{Symbol, Any})
> @ Makie ~/.julia/packages/Makie/GtFuI/src/interfaces.jl:271
> [5] _create_plot(::Function, ::Dict{Symbol, Any}, ::Vector{Float64}, ::Vararg{Any})
> @ Makie ~/.julia/packages/Makie/GtFuI/src/figureplotting.jl:316
> [6] lines(::Vector{Float64}, ::Vararg{Any}; kw::@Kwargs{color::Symbol, linewidth::Int64, axis::@NamedTuple{…}})
> @ MakieCore ~/.julia/packages/MakieCore/UpNdE/src/recipes.jl:436
> [7] top-level scope
> @ ~/.julia/dev/sandbox/turing_predict/example.jl:77
> Some type information was truncated. Use `show(err)` to see complete types.
> 
> ```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [July 8, 2024, 5:55am UTC](https://discourse.julialang.org/t/how-to-animate-multiple-stochastic-processes-with-glmakie/116718/2 "2024-07-08T05:55:05Z")

</div>

I don’t understand what you’re trying to plot as a line, as you’re passing a vector of times for x but a scalar for y, that’s also what the error message means. Probably your indexing is off and you expected to get a vector?

For the other question, you could try the `series` recipe which is a bit of convenience on top of `lines` for multiple time series.

> **[series | Makie](https://docs.makie.org/stable/reference/plots/series)**
>
> Create impressive data visualizations with Makie, the plotting ecosystem for the Julia language. Build aesthetic plots with beautiful customizable themes, control every last detail of publication quality vector graphics, assemble complex layouts and...

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [July 8, 2024, 10:16pm UTC](https://discourse.julialang.org/t/how-to-animate-multiple-stochastic-processes-with-glmakie/116718/3 "2024-07-08T22:16:56Z")

</div>

Thanks for your reply. `series` seems to be what I neeed, but I still can’t figure out how to integrate it properly with the animation. It works correctly without the animation:

```julia
using GLMakie
using SequentialSamplingModels
# simulate the decision process
model = RDM()
times, evidence = simulate(model)
fig, ax, sp = series(times, evidence')
axislegend(ax)

```

However, I get a similar error for the animation:

```julia
using GLMakie
using SequentialSamplingModels 

# simulate the decision process
model = RDM()
times, evidence = simulate(model)
evidence = evidence'
idx = Observable(1)

# setup plot accumulator for choice 1
ys_1 = @lift(evidence[:,$idx])

fig = series(times, ys_1, color = :blue, linewidth = 4,
    axis = (title = @lift("t = $(round(times[$idx], digits = 1))"),))

indices = 1:length(times)
record(fig, "time_animation.mp4", indices;
        framerate = length(times)) do i
    idx[] = i
end

```

Any ideas?
