2017-07-13 29 views
2

我試圖測試上被列爲下面我jupyter筆記本Matplotlib動畫例如:RuntimeError:在的init_func必須返回藝術家的序列對象

from matplotlib import animation 
# solve the ode problem of the double compound pendulum again 

from scipy.integrate import odeint 
from numpy import cos, sin 

g = 9.82; L = 0.5; m = 0.1 

def dx(x, t): 
    x1, x2, x3, x4 = x[0], x[1], x[2], x[3] 

    dx1 = 6.0/(m*L**2) * (2 * x3 - 3 * cos(x1-x2) * x4)/(16 - 9 * cos(x1-x2)**2) 
    dx2 = 6.0/(m*L**2) * (8 * x4 - 3 * cos(x1-x2) * x3)/(16 - 9 * cos(x1-x2)**2) 
    dx3 = -0.5 * m * L**2 * (dx1 * dx2 * sin(x1-x2) + 3 * (g/L) * sin(x1)) 
    dx4 = -0.5 * m * L**2 * (-dx1 * dx2 * sin(x1-x2) + (g/L) * sin(x2)) 
    return [dx1, dx2, dx3, dx4] 

x0 = [np.pi/2, np.pi/2, 0, 0] # initial state 
t = np.linspace(0, 10, 250) # time coordinates 
x = odeint(dx, x0, t) # solve the ODE problem 

fig, ax = plt.subplots(figsize=(5,5)) 

ax.set_ylim([-1.5, 0.5]) 
ax.set_xlim([1, -1]) 

pendulum1, = ax.plot([], [], color="red", lw=2) 
pendulum2, = ax.plot([], [], color="blue", lw=2) 

def init(): 
    pendulum1.set_data([], []) 
    pendulum2.set_data([], []) 

def update(n): 
    # n = frame counter 
    # calculate the positions of the pendulums 
    x1 = + L * sin(x[n, 0]) 
    y1 = - L * cos(x[n, 0]) 
    x2 = x1 + L * sin(x[n, 1]) 
    y2 = y1 - L * cos(x[n, 1]) 

    # update the line data 
    pendulum1.set_data([0 ,x1], [0 ,y1]) 
    pendulum2.set_data([x1,x2], [y1,y2]) 

anim = animation.FuncAnimation(fig, update, init_func=init, frames=len(t), blit=True) 

# anim.save can be called in a few different ways, some which might or might not work 
# on different platforms and with different versions of matplotlib and video encoders 
#anim.save('animation.mp4', fps=20, extra_args=['-vcodec', 'libx264'], writer=animation.FFMpegWriter()) 
#anim.save('animation.mp4', fps=20, extra_args=['-vcodec', 'libx264']) 
#anim.save('animation.mp4', fps=20, writer="ffmpeg", codec="libx264") 
anim.save('animation.mp4', fps=20, writer="avconv", codec="libx264") 

plt.close(fig) 

然而,當我在筆記本上運行的電池它給我這個錯誤:

RuntimeError: The init_func must return a sequence of Artist objects. 

我不太清楚發生了什麼事情..我沒有看到任何錯誤的init函數。

本示例的鏈接:https://github.com/jrjohansson/scientific-python-lectures/blob/master/Lecture-4-Matplotlib.ipynb 它位於本筆記本的底部。

+0

你在哪裏找到這個例子的兩行呢?你能鏈接到它嗎? – ImportanceOfBeingErnest

+0

可能是這個例子一直在matplotlib 1.4.3中工作,但是更新版本的matplotlib已經過時了,如下面的答案中詳細描述的那樣。 – ImportanceOfBeingErnest

回答

0

由於錯誤提示,並且可以看到例如,在simple_animation example中,但也從FuncAnimation documentation,init_func以及更新func應該返回藝術家迭代動畫。

該文檔沒有說這實際上只在使用blit=True時才需要,但由於您在此使用blitting,因此絕對需要。

什麼,因此,你需要做的就是讓init功能以及在update的函數返回其中獲得動畫

def init(): 
    pendulum1.set_data([], []) 
    pendulum2.set_data([], []) 
    return pendulum1, pendulum2, 

def update(n): 
    # n = frame counter 
    # calculate the positions of the pendulums 
    x1 = + L * sin(x[n, 0]) 
    y1 = - L * cos(x[n, 0]) 
    x2 = x1 + L * sin(x[n, 1]) 
    y2 = y1 - L * cos(x[n, 1]) 

    # update the line data 
    pendulum1.set_data([0 ,x1], [0 ,y1]) 
    pendulum2.set_data([x1,x2], [y1,y2]) 

    return pendulum1, pendulum2, 
+0

啊,我明白了!謝謝! – Yatong

相關問題