2011-12-26 18 views
0

こんにちはすべてとメリークリスマス、matplotlibの[パイソン]:

アニメーションの例を説明するのに役立つが、誰かがコードの次のサンプルがどのように動作するかを私に説明してくださいでした(http://matplotlib.sourceforge.net/examples/animation /random_data.html)?

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


timeline = [1,2,3,4,5,6,7,8,9,10] ; 
metric = [10,20,30,40,50,60,70,80,90,100] ; 

fig = plt.figure() 
window = fig.add_subplot(111) 
line, = window.plot(np.random.rand(10)) 

def update(data): 
    line.set_ydata(data) 
    return line, 

def data_gen(): 
    while True: 
     yield np.random.rand(10) 


ani = animation.FuncAnimation(fig, update, data_gen, interval=5*1000) 
plt.show() 

特に、リストを更新するためにリスト(「メトリック」)を使用したいと考えています。 問題は、私が間違っていない場合、FuncAnimationはジェネレータを使用していますが、どうすれば動作させることができますか?

ありがとうございます。

答えて

1

FuncAnimationには、ジェネレータだけでなく繰り返し可能なものを使用できます。 From docs

クラスmatplotlib.animation.FuncAnimation(図、FUNC、フレーム=なし、 init_func =なし、fargs =なし、save_count =なし、** kwargsから)

繰り返しによりアニメーションを作り関数funcを呼び出し、fargsに (オプション)引数を渡します。 フレームは、ジェネレータ、繰り返し可能、 またはフレーム数です。 init_funcは、フレーム をクリアするために使用される関数です。指定されていない場合は、フレームシーケンス の最初の項目から描画した結果が使用されます。

このようにリストを持つequivalenコードは次のようになります。

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

start = [1, 0.18, 0.63, 0.29, 0.03, 0.24, 0.86, 0.07, 0.58, 0] 

metric =[[0.03, 0.86, 0.65, 0.34, 0.34, 0.02, 0.22, 0.74, 0.66, 0.65], 
     [0.43, 0.18, 0.63, 0.29, 0.03, 0.24, 0.86, 0.07, 0.58, 0.55], 
     [0.66, 0.75, 0.01, 0.94, 0.72, 0.77, 0.20, 0.66, 0.81, 0.52] 
     ] 

fig = plt.figure() 
window = fig.add_subplot(111) 
line, = window.plot(start) 

def update(data): 
    line.set_ydata(data) 
    return line, 

ani = animation.FuncAnimation(fig, update, metric, interval=2*1000) 
plt.show()