[英]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()
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.