# Converting an array of matrices into a gif

**URL:** https://discourse.julialang.org/t/converting-an-array-of-matrices-into-a-gif/35741
**Category:** General Usage
**Created:** [March 9, 2020, 3:15am UTC](https://discourse.julialang.org/t/converting-an-array-of-matrices-into-a-gif/35741 "2020-03-09T03:15:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [March 9, 2020, 3:15am UTC](https://discourse.julialang.org/t/converting-an-array-of-matrices-into-a-gif/35741/1 "2020-03-09T03:15:30Z")

</div>

I have an array of 2d matrices and I would like to turn these into a gif. Each of the matrix has an entry from 1 to 8, denoting fixed states. I would like to specify a color for each state, so for example 1 is red, 2 is blue, 3 is green and so on.

The context is an agent-based model of disease spread. Each matrix (100 x 100) represents a square grid environment of individuals and the elements represent the health status. I would like to visualize the spread of disease.

Does anyone have a `Makie.jl`, `Images.jl` or `Reel.jl` tutorial to do this?

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [March 9, 2020, 3:36am UTC](https://discourse.julialang.org/t/converting-an-array-of-matrices-into-a-gif/35741/2 "2020-03-09T03:36:25Z")

</div>

My favourite way to do this is with Interact and Plots, which will give you an “interactive animation”. Use heatmap to do the actual plotting.

If you really want a gif, you can use @gif from Plots, but it’s slow in my experience.

Makie has a record function to do this.

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [March 9, 2020, 3:46am UTC](https://discourse.julialang.org/t/converting-an-array-of-matrices-into-a-gif/35741/3 "2020-03-09T03:46:08Z")

</div>

Interactivity is nice, but I want to email or host the gifs/webm figures. Plus how to do a heatmap with fixed colors?

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [March 9, 2020, 9:10am UTC](https://discourse.julialang.org/t/converting-an-array-of-matrices-into-a-gif/35741/4 "2020-03-09T09:10:35Z")

</div>

```julia
using IndirectArrays, FileIO, Colors
idxarray = rand(1:3, 100, 100, 50);
colorarray = IndirectArray(idxarray, [colorant"red", colorant"purple", colorant"indigo"])
save("myanimation.gif", colorarray; fps=10)

```

(You need ImageMagick.jl for this.)

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [March 11, 2020, 1:56am UTC](https://discourse.julialang.org/t/converting-an-array-of-matrices-into-a-gif/35741/5 "2020-03-11T01:56:25Z")

</div>

> [@tim.holy](#):
>
> colorarray = IndirectArray(idxarray, [colorant"red", colorant"purple", colorant"indigo"])

I am getting some errors with this… mainly that each matrix can have unique numbers from 0 to 8 (like in your example, each matrix will have a number from 1:3). Except, my matrices are little bit different. The “first” matrix is all zeros. The second matrix may have a 1 in there. The third matrix may have 0, 1, and 2… and so on. (Basically the matrices evolve over time).

If the first matrix is all zeros (or not exactly 9 unique elements), then I get the following error:

```julia
ERROR: BoundsError: attempt to access 10-element Array{RGB{N0f8},1} with eltype RGB{FixedPointNumbers.Normed{UInt8,8}}

```

In your example, if you just do `idxarray[:, :, 1] = zeros(Int64, 100, 100)` you will get the same error.

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [March 11, 2020, 2:38pm UTC](https://discourse.julialang.org/t/converting-an-array-of-matrices-into-a-gif/35741/6 "2020-03-11T14:38:07Z")

</div>

Since some of your entries are zero, you need to supply a lookup array that can be indexed with 0. Try OffsetArrays.
