matplotlib animation blit=True causes KeyError

1.1k views Asked by At

I've created some animation which worked fine - albeit - slow when setting the keyword:

blit=False

However, in my efforts to speed up the animation via setting blit=True I came across some strange KeyError exception on running my tests.

Finally, I had a hunch it might not have to do with a coding bug, but maybe with a setting or even a bug.

Thus I've imported the simple_anim.py script from here and found that the same error occurred.

I've tested some more examples and they all gave the same exception.... :(

can anybody help me out and give some information on what's going on?

The code is the following:

"""
A simple example of an animated plot
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))


def animate(i):
    line.set_ydata(np.sin(x + i/10.0))  # update the data
    return line,


# Init only required for blitting to give a clean slate.
def init():
    line.set_ydata(np.ma.array(x, mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
                              interval=25, blit=True)
plt.show()

The raised exception is:

Traceback (most recent call last):
  File "/home/dj754/anaconda3/lib/python3.5/site-packages/matplotlib/backend_bases.py", line 1305, in _on_timer
    ret = func(*args, **kwargs)
  File "/home/dj754/anaconda3/lib/python3.5/site-packages/matplotlib/animation.py", line 1021, in _step
    still_going = Animation._step(self, *args)
  File "/home/dj754/anaconda3/lib/python3.5/site-packages/matplotlib/animation.py", line 827, in _step
    self._draw_next_frame(framedata, self._blit)
  File "/home/dj754/anaconda3/lib/python3.5/site-packages/matplotlib/animation.py", line 845, in _draw_next_frame
    self._pre_draw(framedata, blit)
  File "/home/dj754/anaconda3/lib/python3.5/site-packages/matplotlib/animation.py", line 858, in _pre_draw
    self._blit_clear(self._drawn_artists, self._blit_cache)
  File "/home/dj754/anaconda3/lib/python3.5/site-packages/matplotlib/animation.py", line 898, in _blit_clear
    a.figure.canvas.restore_region(bg_cache[a])
KeyError: <matplotlib.axes._subplots.AxesSubplot object at 0x7fb3b9d3a198>
1

There are 1 answers

0
Joris On BEST ANSWER

After some debugging at home, I've found the answer to my own question. I suspect it is a bug in ipython, but I do not want to state this with certainty, since 99+% of the time, the error is between the screen and the back of the chair in front of this screen.

It turns out that it originated from what I did before running the script I've provided myself: turning interactive on via plt.ion()

This was a newbie mistake, which I've since corrected. However, the script I've ran before the simple_anim.py was called in IPython3 via run simple_anim.py' (I prefer developing with IPython.. legacy of my MATLAB experiences). I've forgotten to mention this, but this turned out to be critical. If I had tried running the code *the normal way* viapython3 simple_anim.py' everything would've worked fine!

However, it turns out that setting interactive mode to on, while being in IPython was the cause of the exception.

I am very aware that enabling interactive mode in IPython via plt.ion() is hugely stupid, however, it happened to me, and I suspect more people have suffered from this. It therefore think this behaviour (raising such a KeyError exception) is at least unwanted behaviour, and probably a bug in Ipython or the matplotlib.pyplot.ion() function.

Has anybody got any ideas on how and if I should mention this maybe-bug?