matplotlib funcAnimation没有调用传递的函数

Jos*_*uaF 2 python matplotlib

我正在尝试使机器人的链接动画化,但没有成功。FuncAnimation永远不会调用动画函数-永远不会执行print语句。任何帮助将不胜感激。我的代码:

joints = np.array([robot_kinematics.getJoints(a[0]) for a in path])
# this is [5x9x3]

fig    = plt.figure()
ax     = fig.add_subplot(111, projection='3d')
colors = 'brgymcwkk'
lines  = [ax.plot([], [], [])[0] for i,c in enumerate(colors)]
pt     = ax.plot([], [], [])[0] 


def animate(i,lines,pts):
   print ('called')
   for j,line in enumerate(lines):
       line.set_data(joints[i,j,0:2])
       line.set_3d_properties(joints[i,j,2])
   pts.set_data(joints[i,:,0:2])
   pts.set_3d_properties(joints[i,:,2])

   return lines,pts

a = animation.FuncAnimation(fig, animate, 25, fargs=(lines,pt),interval=50, blit=False)    
plt.show()
Run Code Online (Sandbox Code Playgroud)

Jos*_*uaF 6

必须将FuncAnimation创建的对象显然分配给全局变量。如果将其分配给局部变量(如我在此处所做的那样),则不会发生任何事情。

  • 不知道为什么会发生这种情况,但可以确认这是真的。谢谢@JoshuaF。编辑:来自 https://matplotlib.org/api/animation_api.html 动画由计时器(通常来自主机 GUI 框架)推进,Animation 对象持有该计时器的唯一引用。如果您不持有对 Animation 对象的引用,它(以及计时器)将被垃圾收集,这将停止动画。 (2认同)