# Animation of a GraphPlots in an Array

**URL:** https://discourse.julialang.org/t/animation-of-a-graphplots-in-an-array/94793
**Category:** Graphs
**Tags:** plotting, graphs, animations
**Created:** [February 17, 2023, 3:34pm UTC](https://discourse.julialang.org/t/animation-of-a-graphplots-in-an-array/94793 "2023-02-17T15:34:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![flobe](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@flobe](https://discourse.julialang.org/u/flobe)
#### Post date: [February 17, 2023, 3:34pm UTC](https://discourse.julialang.org/t/animation-of-a-graphplots-in-an-array/94793/1 "2023-02-17T15:34:48Z")

</div>

Hi all,

I have a big simulation loop resulting in a graphplot for each timestep. I store each of those plots in an array. Ultimately, I’d like to animate over all entries in this array. The minimal working example looks somehow like this:

```julia
using GraphPlot

gplotvec = Array{Any}(nothing, 100)

for i in 1:100
    g = graphfamous("karate")
    gplotvec[i] = gplot(g)
return gplotvec
end

```

I tried different things like initializing an animation object and storing each element of the array in a frame of the animation object - doesn’t work. The problem could be that the array elements are of type `context` rather than type `plot`(?).

Any help is highly appreciated 😊

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [February 18, 2023, 10:00am UTC](https://discourse.julialang.org/t/animation-of-a-graphplots-in-an-array/94793/2 "2023-02-18T10:00:25Z")

</div>

Hi. I can’t help you with GraphPlot, sorry. But I can show you how I make animated graphs using Karnak.jl.

![dots](https://global.discourse-cdn.com/julialang/original/3X/6/d/6d6725a1dd4c64b31dcae1649edfc033383a2f73.gif)

Using your setup of creating a vector of graphs, each of the 20 frames shows one of the graphs and NetworkLayout.jl’s `stress` algorithm coping well enough, most of the time.

> **code for gif**
>
> ```
> using Graphs
> using Karnak
> 
> nG = 20
> 
> gplotvec = Graph[]
> for i in 1:nG
> push!(gplotvec, grid([i, i + 1]))
> end
> 
> cols = Luxor.Colors.JULIA_LOGO_COLORS
> 
> function frame(scene, framenumber)
> background("black")
> sethue("grey30")
> drawgraph(gplotvec[framenumber], 
> vertexfillcolors = (v) -> cols[mod1(v, end)],
> vertexshapesizes = 6,
> layout=spring)
> sethue("white")
> text(string(framenumber), boxtopleft() * 0.9)
> end
> 
> movie = Movie(800, 500, "dots")
> animate(movie, [Scene(movie, frame, 1:nG)], 
> framerate=3,
> creategif=true,
> pathname="/tmp/dots.gif")
> 
> ```

There’s plenty of documentation, and feel free to open an issue on github if it’s not enough.

Alternatively, you can investigate GraphMakie.jl, which I’d guess ties into Makie’s animation system.

---

<div class="post-metadata">

### Author: ![flobe](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@flobe](https://discourse.julialang.org/u/flobe)
#### Post date: [March 3, 2023, 1:37pm UTC](https://discourse.julialang.org/t/animation-of-a-graphplots-in-an-array/94793/3 "2023-03-03T13:37:34Z")

</div>

Thanks very much, @cormullion for your reply! Somehow I found a workaround using `compose.jl` (already used in `graphplot.jl`).

```julia
anim = Animation()

for i in 1:500
	cur_timestep = round(timestep * i, digits = 2)
	output = compose(gplotvec[i],
		(context(), rectangle(), fill("grey")))
	j = length(anim.frames) + 1
	tmpfilename = joinpath(anim.dir, @sprintf("%06d.png", j))
	Compose.draw(PNG(tmpfilename), output)
	push!(anim.frames, tmpfilename)
end

gif(anim, "anim_test.gif", fps = 20)

```
