2016-05-09 52 views
2

animation()関数に引数を渡す方法は? 、私はしようとしたが、dintが働いた。 animation.FuncAnimationの プロトタイプは()animation.FuncAnimation()の引数の受け渡し

クラスmatplotlib.animation.FuncAnimation(図、FUNC、フレーム=なし、init_func =なし、fargs =なし、save_count =なし、** kwargsから)塩基である:matplotlibの。 animation.TimedAnimation

私は以下のコードを貼り付けました。変更が必要ですか?

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

def animate(i,argu): 
    print argu 

    graph_data = open('example.txt','r').read() 
    lines = graph_data.split('\n') 
    xs = [] 
    ys = [] 
    for line in lines: 
     if len(line) > 1: 
      x, y = line.split(',') 
      xs.append(x) 
      ys.append(y) 
     ax1.clear() 
     ax1.plot(xs, ys) 
     plt.grid() 

ani = animation.FuncAnimation(fig,animate,fargs = 5,interval = 100) 
plt.show() 
+0

「何がうまくいかなかったか」を説明できますか?あなたは痕跡を戻しましたか? – tacaswell

答えて

3

チェックこの単純な例:

# -*- coding: utf-8 -*- 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import numpy as np 

data = np.loadtxt("example.txt", delimiter=",") 
x = data[:,0] 
y = data[:,1] 

fig = plt.figure() 
ax = fig.add_subplot(111) 
line, = ax.plot([],[], '-') 
line2, = ax.plot([],[],'--') 
ax.set_xlim(np.min(x), np.max(x)) 
ax.set_ylim(np.min(y), np.max(y)) 

def animate(i,factor): 
    line.set_xdata(x[:i]) 
    line.set_ydata(y[:i]) 
    line2.set_xdata(x[:i]) 
    line2.set_ydata(factor*y[:i]) 
    return line,line2 

K = 0.75 # any factor 
ani = animation.FuncAnimation(fig, animate, frames=len(x), fargs=(K,), 
           interval=100, blit=True) 
plt.show() 

まず、データ処理のため、numpyのを使用することをお勧めします最も簡単読み込まれ、データを書き込みます。

各アニメーションステップで "plot"関数を使用する必要はありませんが、代わりにset_xdataset_ydataメソッドを使用してデータを更新します。

Matplotlibのドキュメントの例も参照してください。http://matplotlib.org/1.4.1/examples/animation/

+0

素晴らしい答えのためにホルヘに感謝します。しかし、キャンバスをwxpython形式で埋め込んだら、set_xdataとset_ydataメソッドは動作しません。何か考えていますか? – vinaykp

+0

wxPythonのバックエンドを使用して、このミニアダプテーションを確認してください。最後の例は、[http://pastebin.com/c1WSRRD7](http://pastebin.com/c1WSRRD7) –

+0

ありがとうございます。私はurの実装のように同じことをしましたが、ドロップダウンリストからオプションを選択したときにanimate()をトリガーしました。したがって、ドロップダウンリストanimate()関数から選択されたオプションが、指定された時間ごとに繰り返し実行されました。しかし、画面に何も表示されませんでした。それがなぜ起こるかあなたは何か考えがありますか? – vinaykp

1

私は、あなたはかなりそこだと思う、次のようにいくつかのマイナーな改良を持っている基本的にあなたがフィギュアを定義する必要があり、軸のハンドルを使用して、リスト内のfargsを入れ、

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

fig, ax1 = plt.subplots(1,1) 

def animate(i,argu): 
    print(i, argu) 

    #graph_data = open('example.txt','r').read() 
    graph_data = "1, 1 \n 2, 4 \n 3, 9 \n 4, 16 \n" 
    lines = graph_data.split('\n') 
    xs = [] 
    ys = [] 
    for line in lines: 
     if len(line) > 1: 
      x, y = line.split(',') 
      xs.append(float(x)) 
      ys.append(float(y)+np.sin(2.*np.pi*i/10)) 
     ax1.clear() 
     ax1.plot(xs, ys) 
     plt.grid() 

ani = animation.FuncAnimation(fig, animate, fargs=[5],interval = 100) 
plt.show() 

私はファイルがなく、iの依存関係が追加されたので、プロットが動くので、example.txtをハードワイヤードの文字列に置き換えてください。

関連する問題