我的动画没有按预期工作:
使用blit = True我总是在屏幕上显示原始功能,如果没有它,我会对功能进行更新,但这两种功能都不可取。
任何帮助非常感谢,我使用Spyder IDE在Win7上使用Anaconda SciPy软件包集合
我已尝试使用animation.FuncAnimation()中的参数进行播放但没有运气,我已经将代码绑定了。
Bool=
答案 0 :(得分:0)
设置init
功能为required "to set a clean slate":
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
def run():
fig = plt.figure()
pathcol = plt.scatter([], [])
def init():
pathcol.set_offsets([[], []])
return [pathcol]
def update_plot(i, pathcol):
x = range(i, 100+i)
y = range(i, 1000+i, 10)
pathcol.set_offsets([(xi, yi) for xi, yi in zip(x, y)])
return [pathcol]
plt.xlim(-10, 200)
plt.ylim(-100, 1500)
ani = animation.FuncAnimation(fig, update_plot,
init_func=init,
interval=0,
blit=True, fargs=[pathcol])
plt.show()
run()
此外,在update_plot
内请务必使用pathcol.set_offsets
来修改
现有 PathCollection
,而不是再次调用plt.scatter
。修改
现有的Artist
将提高动画速度。
init
不接受任何参数,但我们希望init
引用pathcol
中创建的run
。因此,我在init
函数中移动了run
,以便init
内部pathcol
在run
的封闭范围内找到update_plot
。
pathcol
自fargs=[pathcol]
后传递update_plot
,因此run
可以成为init
之外的函数。但由于run
嵌套在update_plot
内,为了保持对称性,我决定将run
置于{{1}}内。