Animation in Jupyter with Binder

Hi all.

I try to use binder to have an interactive animation of python on jupyter. Everything works well offline, but when I run the matplotlib magic command to allow interactive detached plots:
$ %matplotlib tk

I get following error:
$ ImportError: Cannot load backend ‘TkAgg’ which requires the ‘tk’ interactive framework, as ‘headless’ is currently running

What I use involves using the classic notebook interface.
Then you put %matplotlib notebook at the first line of the notebook cell and have the code for your animated matplotlib plot or plots and subplots below. As it runs it will show it in the notebook output area.

This won’t work in JupyterLab as the %matplotlib inline approach doesn’t make an updating plot. I just get the end result if I have it set to loop a set amount. And the %matplotlib notebook presently fails in JupyterLab.

There is a demo notebook launchable from here. (For direct to Binder session link, click here.) I basically loop and update that plot with new data, clearing the old with ax.clear() and control that with a sleep interval.
There is a package to do animated plots in notebooks at https://animatplot.readthedocs.io/en/latest/gallery/Nuke.html. I haven’t looked at the behind the scenes approach to see how it compares to what I had working.

There is also this by Parul Pandey. While making the repo compatible with running from MyBinder would help, if you troubleshoot and modify it some to run from the MyBinder link, it demonstrates some interesting approaches such as the celluloid module and also hints at how you could go about making videos of matplotlib animations.

2 Likes