Plots (GR backend): animate a moving window

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, 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).

Do you have a MWE for that problem?

1 Like

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:

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

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.

# 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")