2016-03-07 177 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