# Animated image to illustrate sequence of moves

**URL:** <https://discourse.julialang.org/t/animated-image-to-illustrate-sequence-of-moves/81412>\
**Category:** Visualization\
**Tags:** plots, animations\
**Created:** [May 21, 2022, 11:16am UTC](https://discourse.julialang.org/t/animated-image-to-illustrate-sequence-of-moves/81412 "2022-05-21T11:16:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 21, 2022, 11:16am UTC](https://discourse.julialang.org/t/animated-image-to-illustrate-sequence-of-moves/81412/1 "2022-05-21T11:16:02Z")

</div>

I would like to know if it is possible to show the solutions of [this problem](https://discourse.julialang.org/t/from-recursive-to-iterative/81411) in a dynamic way with an animated sequence of images that illustrate the various steps of the solution sequence.  
In summary I have a nxn table, I would like to show an animation that somehow highlights the various cells according to a sequence determined by a given vector of positions.

for example, make the following sequence “animate” in the 5x5 table

```julia
julia> tab=CartesianIndices((1:n,1:n))
5×5 CartesianIndices{2, Tuple{UnitRange{Int64}, UnitRange{Int64}}}:
 CartesianIndex(1, 1) CartesianIndex(1, 2) … CartesianIndex(1, 5)
 CartesianIndex(2, 1) CartesianIndex(2, 2) CartesianIndex(2, 5)
 CartesianIndex(3, 1) CartesianIndex(3, 2) CartesianIndex(3, 5)
 CartesianIndex(4, 1) CartesianIndex(4, 2) CartesianIndex(4, 5)
 CartesianIndex(5, 1) CartesianIndex(5, 2) CartesianIndex(5, 5)

julia> branch1(init)
552-element Vector{Any}:
 CartesianIndex{2}[CartesianIndex(1, 1), CartesianIndex(4, 1), CartesianIndex(4, 4), CartesianIndex(1, 4), CartesianIndex(3, 2), CartesianIndex(3, 5), CartesianIndex(5, 3), CartesianIndex(2, 3), CartesianIndex(4, 5), CartesianIndex(1, 5) … CartesianIndex(2, 1), CartesianIndex(4, 3), CartesianIndex(1, 3), CartesianIndex(3, 1), CartesianIndex(3, 4), CartesianIndex(5, 2), CartesianIndex(2, 2), CartesianIndex(2, 5), CartesianIndex(5, 5), CartesianIndex(3, 3)] 

```

---

<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:** [May 21, 2022, 4:28pm UTC](https://discourse.julialang.org/t/animated-image-to-illustrate-sequence-of-moves/81412/2 "2022-05-21T16:28:32Z")

</div>

Would something like this suit you?

![Plots_scatter_CartesianIndices](https://global.discourse-cdn.com/julialang/original/3X/a/7/a7fa738dd6313c02078cd72347ed957ff40c8cfb.gif)

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 22, 2022, 5:29am UTC](https://discourse.julialang.org/t/animated-image-to-illustrate-sequence-of-moves/81412/3 "2022-05-22T05:29:08Z")

</div>

Yes. I’m looking for just such a thing.  
Was it produced with some package of julia?  
Would it be possible to add for each colored dot its position in the sequence? That is 1 for the first, 2 for the second, … nxn for the last?

---

<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:** [May 22, 2022, 6:48am UTC](https://discourse.julialang.org/t/animated-image-to-illustrate-sequence-of-moves/81412/4 "2022-05-22T06:48:15Z")

</div>

> [@rocco\_sprmnt21](#):
>
> Would it be possible to add for each colored dot its position in the sequence? That is 1 for the first, 2 for the second, … nxn for the last?

Using Plots.jl, you could do:

![Plots_scatter_CartesianIndices](https://global.discourse-cdn.com/julialang/original/3X/2/7/27e01c8da868a3dadea9777de494e28f18e57d2e.gif)

> **Code**
>
> ```julia
> # ... resuming here from your code:
> n = 5
> init = CartesianIndex(1,1)
> tab = CartesianIndices((1:n,1:n))
> y = branch(init)
> 
> using Plots
> t0 = Tuple(init)
> scatter(vec(Tuple.(tab)), mc=:white, msc=:black, ms=10, lims=(0.5,n+0.5), ratio=1)
> anim1 = @animate for (i,yi) in pairs(y[1]) # consider first sequence y[1] of 25 moves
> scatter!(t0, mc=:grey, msc=:red, ms=10)
> ti = Tuple(yi)
> scatter!(ti, c=:red, msc=:red, ms=10)
> global t0 = ti
> annotate!((t0 .+ (0,-1/n))...,text("$i","Computer Modern",8,:top,:black))
> end
> gif(anim1, "Plots_scatter_CartesianIndices.gif", fps=1)
> 
> ```

---

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [May 23, 2022, 7:13am UTC](https://discourse.julialang.org/t/animated-image-to-illustrate-sequence-of-moves/81412/5 "2022-05-23T07:13:40Z")

</div>

Which backend are you using with Plots.jl? I don’t see it explicitly defined in your example.

BTW, excellent example and clarity 👍🏽

---

<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:** [May 23, 2022, 7:20am UTC](https://discourse.julialang.org/t/animated-image-to-illustrate-sequence-of-moves/81412/6 "2022-05-23T07:20:30Z")

</div>

When you start with a fresh Julia session, `using Plots` uses the `gr()` backend by default, so we can ommit it.
