Matplotlib animation doesn't work in spyder

I am new to python and stackoverflow and I will move on to examples in matplotlib. I was looking for a solution to this problem with no luck, although I managed to find a previously unanswered question in stackoverflow with the same problem.

Basically, I copied the code available from the examples into matplotlib ; eg:

import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation def data_gen(t=0): cnt = 0 while cnt < 1000: cnt += 1 t += 0.1 yield t, np.sin(2*np.pi*t) * np.exp(-t/10.) def init(): ax.set_ylim(-1.1, 1.1) ax.set_xlim(0, 10) del xdata[:] del ydata[:] line.set_data(xdata, ydata) return line, fig, ax = plt.subplots() line, = ax.plot([], [], lw=2) ax.grid() xdata, ydata = [], [] def run(data): # update the data t, y = data xdata.append(t) ydata.append(y) xmin, xmax = ax.get_xlim() if t >= xmax: ax.set_xlim(xmin, 2*xmax) ax.figure.canvas.draw() line.set_data(xdata, ydata) return line, ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10, repeat=False, init_func=init) plt.show() 

I run various animation examples in Anaconda 2 (python 2.7) and 3 (python 3.5), and both give me an empty plot without animation. However, each animation works great in Enthought Canopy.

Is there something simple that I don't see when using spyder?

+8
python matplotlib animation
source share
1 answer

You need to change the backend to run the animation in the IPython console. You can do this by running the %matplotlib qt before animating.

If you do not want to use this command every time, you can go to: Tools > Preferences > IPython Console > Graphics > Backend and change it from "Inline" to "Automatic" .

Update: February 2018, it is now in python> Preferences In the window, select the IPython console in the LH panel of the window. Select the "Graphics" tab and there is a backend.

Read this for more details.

+9
source share

All Articles