matplotlib分散动画;原始情节始终在屏幕上

时间:2015-08-13 16:31:30

标签: python matplotlib

我的动画没有按预期工作:

使用blit = True我总是在屏幕上显示原始功能,如果没有它,我会对功能进行更新,但这两种功能都不可取。

任何帮助非常感谢,我使用Spyder IDE在Win7上使用Anaconda SciPy软件包集合

我已尝试使用animation.FuncAnimation()中的参数进行播放但没有运气,我已经将代码绑定了。

Bool=

1 个答案:

答案 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内部pathcolrun的封闭范围内找到update_plot

  • pathcolfargs=[pathcol]后传递update_plot,因此run可以成为init之外的函数。但由于run嵌套在update_plot内,为了保持对称性,我决定将run置于{{1}}内。