Skip to content

Animations & Object Persistence #1656

@jakevdp

Description

@jakevdp

Unlike other plotting operations, if you create an animation and do not store it in a persistent variable, it will fail.

So, for example,

import matplotlib.pyplot as plt
from matplotlib import animation

fig = plt.figure()

def animate(i):
   # ... do something here

anim = animation.FuncAnimation(fig, animate)
plt.show()

will work, but changing the final lines to

animation.FuncAnimation(fig, animate)
plt.show()

(i.e. not creating a variable to store the animation) leads to a quiet failure.

I think the problem is because the figure does not store a reference to the animation object, python's garbage collection removes it if it is not explicitly stored.
Probably the best way to address this would be to add an animation attribute to the Figure class, such that any animation will store a pointer to itself in its associated figure.

I think this is a pretty easy fix, but I'm just wondering if there are any other details to be aware of when adding something like this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions