Animating Matplotlib with Qt support in jupyter

It seems that the Qt backend cannot be used in Jupyter to animate a plot. Is it true at all?

The code shown below works fine using a backend Qt4Agg

or TkAgg

in a script. It also works great using notebook ( %matplotlib notebook

) or tk ( %matplotlib tk

) backend in Jupyter notebook.

However, when used %matplotlib qt

(or %matplotlib qt4

) in Jupyter, the window freezes and the kernel dies.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation 

y = np.cumsum(np.random.normal(size=30))

fig, ax = plt.subplots()
line, = ax.plot(np.arange(len(y)),y)
ax.set_xlim(0,30)
ax.set_ylim(y.min(),y.max())

def update(i):
    x = np.arange(i)
    line.set_data(x,y[:i])

ani = matplotlib.animation.FuncAnimation(fig, update, frames=30, repeat=False)
plt.show()

      

By commenting out the line ani = matplotlib.animation.FuncAnimation(...)

, a window appears and remains responsive for the backend in use as well %matplotlib qt

. So it %matplotlib qt

doesn't seem to work with animations in Jupyter.

I am using python 2.7, matplotlib 2.0, notepad 4.4.1.

My question is:

  • Is this behavior expected?
  • Can anyone reproduce this?
  • Is Qt backend really invalid in Jupyter with matplotlib animations?
  • And if so, does anyone know the reason for this?
+3


source to share





All Articles