【问题标题】:RuntimeError: The init_func must return a sequence of Artist objectsRuntimeError: init_func 必须返回 Artist 对象序列
【发布时间】:2017-07-13 22:06:23
【问题描述】:

我正在尝试在我的 jupyter 笔记本上测试一个 Matplotlib 动画示例,如下所示:

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 它位于本笔记本的最底部。

【问题讨论】:

  • 你在哪里找到那个例子?能给个链接吗?
  • 可能是这个例子一直在 matplotlib 1.4.3 中工作,但是对于较新版本的 matplotlib,已经过时了,如下面的答案所述。

标签: python matplotlib


【解决方案1】:

正如错误所暗示的那样,可以看出例如在simple_animation example 中,也来自FuncAnimation documentationinit_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, 

【讨论】:

    猜你喜欢
    • 2020-10-28
    • 2019-12-31
    • 1970-01-01
    • 2020-10-09
    • 2016-01-17
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多