Interaction with animation widgets in matplotlib

Using matplotlib with the %matplotlib widget magic displays figures as widgets, but there seems to be very little interaction with the animation. It would be nice to have animation controls, but to achieve this I’ve found I have to go around the widget provided and create my own interface, linking up the controls to the animation. Is there a better way to achieve this?

Since you don’t specifically provide a description of what you are trying to do or a minimal toy code example (always better!), I’ll try to raise some issues and may you can provide more direction in a reply…

I think you are perhaps mixing concepts? Interaction isn’t necessarily animation. And you still have to code in how the interaction works. The ipywidgets interact and interactive eliminate some of the nitty-gritty of this if your needs can be met by the options that come with those. See here.
(Most of these require an active kernel. This distinction becomes particularly important when discussing animations below.)

If you truly want an animation, that can be done differently and doesn’t need %matplotlib notebook or %matplotlib widget or %matplotlib ipympl, you can use animation.FuncAnimation() with the ability to make frames from the animation send the animation to js_html() related control. That gives you a widget controller that will work to control the frames showing in all sorts of way. Importantly, that widget controller works with javascript and so it doesn’t need an active kernel and so maintains the interactive nature even without a kernel and so works in the ‘static’ versions of the notebook you can view vie nbviewer. A ‘static’ view of this notebook illustrates this. Go there and use the controller on that page. You can scrub through or play it back in all sorts of ways. To see how to do this, go to here and press ‘launch binder’. Then scroll down to where I mention ‘Louis Tiao’ and work through that part. (Pardon the present state of that section. It is a bit of a mess at present. In particular, in a later part I either pointed to the wrong Stackoverflow post or some overzealous moderator deleted my very detailed comments offering examples where the code would work in an active notebook without the controller, even using Qt. And way down in an unrelated section is a dead link and someone removed a Stackoverflow post since I linked to it. I’ll endeavor to improve it again sometime.)

1 Like

Thanks so much for the pointers. Pretty much what I wanted to achieve was done by:

pyplot.rcParams["animation.html"] = "jshtml"

This seems to make the display of the animation object an interactive style interface. But it is exceedingly slow, so I’m not sure if it is going to be a workable solution. The animation seems to be running by default, even if you aren’t displaying the figure it is attached to. So you, somewhat unexpectedly, have to always call pause() on it after evaluation.

I guess, ideally, when the animation object is display()ed in a notebook, then one would get the animation with a way to play/pause/reset it through interaction, much like each figure can be zoomed/panned through interaction.

That isn’t my experience. The example ones that use display(HTML(anim.to_jshtml())) or plt.rcParams["animation.html"] = "jshtml" need me to toggle them using the javascript based widget controller to play in both classic interface and JupyterLab.

Those using javascript for the animation, i.e.,that use display(HTML(anim.to_jshtml())) or plt.rcParams["animation.html"] = "jshtml", do take a while to generate though because they have to generate all the individual frames first. The other methods I describe on that page above mention of the 'Louis Tiao’ don’t generate all the frames and so they play more quickly. However, since they aren’t frame based, you cannot scrub back and forth on demand.

You’ll need to determine which options work best for you. As I point out, once you generate the frames you can make a gif or HTML5-based video you can play elsewhere in a browser, even embedded into Jupyter notebooks that are running actively or ‘static’. The HTML5-based video option including controls.