Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation from matplotlib not working in spyder

Tags:

I'm new to both python and stackoverflow, and I'm going over examples at matplotlib. I've searched for the solution to this problem with no luck, although I was able to locate a previously unanswered question in stackoverflow with the same issue.

Basically, I copied the code available from the examples at matplotlib; for instance:

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've run various animation examples in both Anaconda 2 (python 2.7) & 3 (python 3.5), and both give me a blank plot without animation. However, each animation works perfectly well in Enthought Canopy.

Is there something simple I'm missing when using spyder?

like image 706
Medalgardr Avatar asked Mar 07 '16 23:03

Medalgardr


People also ask

How do you run an animation on Spyder?

You have to change the backend to run an animation in the IPython console. You can do that by running %matplotlib qt command before the animation. If You don't want to use this command every time, You can go to: Tools > Preferences > IPython Console > Graphics > Backend and change it from "Inline" to "Automatic" .

Does matplotlib work in Spyder?

Yes, the setting from the Preferences dialog takes effect for new IPython consoles only; any console tabs already opened are not affected. Yeah, Norman is right: just restart Spyder after changing the Graphics backend in Tools > Preferences. No need to run "%matplotlib qt".

How do you make an animation move in Python?

You can create animations in Python by calling a plot function inside of a loop (usually a for-loop). The main tools for making animations in Python is the matplotlib. animation. Animation base class, which provides a framework around which the animation functionality is built.


1 Answers

You have to change the backend to run an animation in the IPython console. You can do that by running %matplotlib qt command before the animation.

If You don't 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: Feb 2018, this is now in python>Preferences In the window select IPython console in the LH pane of the window. Select the Graphics tab and backend is in there.

For more details please read this.

like image 81
Tony Babarino Avatar answered Sep 23 '22 19:09

Tony Babarino