2016-03-07 134 views
8

我是新来的python和stackoverflow,我会在matplotlib的例子。我已经搜索了解决这个问题的方法,但没有运气,尽管我能够在相同的问题中找到一个带有计算器的previously unanswered question从matplotlib动画不工作在Spyder

基本上,我从matplotlib复制了示例中可用的代码;例如:

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() 

我已经运行在这两个蟒蛇2(Python 2.7版)& 3(蟒蛇3.5)各种动画的例子,无一不给我一个空白的阴谋没有动画。然而,每个动画在Enthought Canopy中都能很好地工作。

使用spyder时,有什么简单的我不知道?

回答

9

您必须更改后端才能在IPython控制台中运行动画。您可以通过在动画之前运行%matplotlib qt命令来完成此操作。

如果你不想每次都用这个命令,你可以去: Tools > Preferences > IPython Console > Graphics > Backend 并改变它从"Inline""Automatic"

更新:2018年2月,这现在在python>首选项在窗口中选择窗口LH窗格中的IPython控制台。选择图形选项卡,后端在那里。

欲了解更多详情,请阅读this

+1

完美,谢谢!只要我有足够的代表,我就会投票。 – Medalgardr

+0

'%matplotlib qt5'为我工作。 – cjorssen