簡體   English   中英

matplotlib分散動畫; 原始圖始終顯示在屏幕上

[英]matplotlib scatter animation; original plot always on screen

我的動畫無法正常工作:

使用blit = True時,我始終會在屏幕上顯示原始功能,沒有它,我會對該功能進行每次更新,但都不希望這樣做。

我非常感謝任何幫助,我在帶有spyder IDE的Win7上使用了Anaconda SciPy軟件包的Anaconda集合

我試過了animation.FuncAnimation()中的參數,但是沒有運氣,我勉強掌握了代碼。

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation



def run():

    x = range(100)
    y = range(0, 1000, 10)
    x2 = range(50)
    y2 =range(0, 500, 10)
    fig = plt.figure()
    scat1 = plt.scatter(x, y)


    ani = animation.FuncAnimation(fig, update_plot, blit = True)
    plt.show()

def update_plot(i):
    x = range(i, 100+i)
    y = range(i, 1000+i, 10)
    scat1 = plt.scatter(x,y)


    return scat1,


run()

設置init功能是“設置干凈的狀態”必需的

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 Python會發現pathcol在封閉范圍內run

  • 由於fargs=[pathcol]update_plot被傳遞pathcol fargs=[pathcol] ,因此可以使update_plot成為run之外的函數。 但是由於init嵌套在run ,出於對稱性,我決定也將update_plot放入run內。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM