# Plots (GR backend): animate a moving window

**URL:** <https://discourse.julialang.org/t/plots-gr-backend-animate-a-moving-window/24133>\
**Category:** Visualization\
**Tags:** plots, gr, animations\
**Created:** [May 12, 2019, 7:32am UTC](https://discourse.julialang.org/t/plots-gr-backend-animate-a-moving-window/24133 "2019-05-12T07:32:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![taqtiqa-mark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taqtiqa-mark/32/4383_2.png) [@taqtiqa-mark](https://discourse.julialang.org/u/taqtiqa-mark)\
**Post date:** [May 12, 2019, 7:32am UTC](https://discourse.julialang.org/t/plots-gr-backend-animate-a-moving-window/24133/1 "2019-05-12T07:32:43Z")

</div>

Hi,  
Thank you for all the effort that has been put into  
the package Plots and the various backend packages.

I’ve followed the various examples showing how to animate a plot and save it as a GIF.

In the course of those examples I have not come across a way to make the animation plot a moving window, say 30 obsertaions:

- first thirty observations are plotted with the animation updated for each observation
- then, on adding 31st observation, the first observation is removed.
- etc.

For instance, [in the GR example](http://docs.juliaplots.org/latest/examples/gr/#functions-adding-data-and-animations), the PoLS suggets inserting something like `x > 2 ? pop!(p,1) : nothing` but then I just got an error indicating `pop!` is not implemented for plots that have been `push!`ed to.

Appreciate any hints or tips about the canonical way to go about this using Plots (with the GR backend).

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [May 12, 2019, 8:00am UTC](https://discourse.julialang.org/t/plots-gr-backend-animate-a-moving-window/24133/2 "2019-05-12T08:00:13Z")

</div>

Do you have a MWE for that problem?

---

<div class="post-metadata">

**Author:** ![taqtiqa-mark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taqtiqa-mark/32/4383_2.png) [@taqtiqa-mark](https://discourse.julialang.org/u/taqtiqa-mark)\
**Post date:** [May 12, 2019, 9:10am UTC](https://discourse.julialang.org/t/plots-gr-backend-animate-a-moving-window/24133/3 "2019-05-12T09:10:20Z")

</div>

Hi Josef,  
Thanks for looking at this.

If you uncomment the `pop!` line in the following two examples you’ll see an error such as:

```julia
ERROR: MethodError: no method matching pop!(::Plots.Plot{Plots.GRBackend}, ::Int64)
Closest candidates are:
  pop!(::BitSet, ::Integer) at bitset.jl:240
  pop!(::BitSet, ::Integer, ::Any) at bitset.jl:249
  pop!(::DataStructures.IntSet, ::Integer) 

```

The following is the GR example from the [docs](http://docs.juliaplots.org/latest/examples/gr/#functions-adding-data-and-animations)

```julia
p = Plots.plot([sin, cos], zeros(0), leg=false)
anim = Animation()
for x = range(0, stop=10π, length=100)
    push!(p, x, Float64[sin(x), cos(x)])
    #x > 2 ? pop!(p,1) : nothing
    frame(anim)
end
gif(anim, "/tmp/gr-anim-push.gif")

```

Elsewhere I saw the updated syntax is as follows.

```julia
# Animated plots
p=Plots.plot([sin,cos],zeros(0),legend=false)
anim2 = @animate for x=range(0,stop=10π, length=100)
    push!(p,x,Float64[sin(x),cos(x)])
    #x > 2 ? pop!(p,1) : nothing
end
gif(anim2, "/tmp/anim-push.gif")

```
