2017-02-02 7 views
0

アニメーション用の単純なPythonコードを書いています。コードはランダムな点を作成し、アニメーション中にプロットします。Pythonアニメーションの更新

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

x = 10*np.random.rand(10,1) 
y = 10*np.random.rand(10,1) 


fig = plt.figure() 
ax = plt.axes(aspect='equal',xlim =(-10,10), ylim = (-10,10)) 
plts = ax.plot([], [], 'o-') 

def init(): 
    plts.set_data([],[]) 
    return plts 

def animate(num,x,y,plots,skip): 
    plts[0].set_data(x[:num*skip],y[:num*skip]) 
    return plts 

skip = 1 
ani = animation.FuncAnimation(fig, 
           animate, 
           frames=10, 
           fargs =(x,y,plts,skip), 
           interval=1000) 


plt.show() 

アニメーション中、コードはすべての点をプロットします。 フレーム内の1つのポイントだけをプロットし、前のポイントをクリアする方法を教えてもらえますか? Random point plot animation output

答えて

1

リストのすべての点をプロットすると、インデックスnum*skipまで表示されます。各フレームnumは1だけ増加し、したがって1つの追加ポイントがプロットされます。

のみnum番目の点をプロットするために、単に使用

plts[0].set_data(x[num],y[num]) 
関連する問題