2016-08-23 2 views
0

私は自分のプロジェクトのためにPythonでアニメートする方法を学習しています。次の例のコードをhereから外しています。Python FuncAnimationが更新を認識しない

import numpy as np 
import h5py, os, glob, sys, time 
import matplotlib as mpl 
mpl.use('Agg') 
import matplotlib.pyplot as plt 
from matplotlib.animation import FuncAnimation 

def update(i): 
    for j in np.arange(0,10): 
     for k in np.arange(0,10): 
      for channel in ["N","E"]: 
       x = some_x_value 
       y = some_y_value 
       line = plt.loglog(x,y) 
       ax.set_xlabel(label) 
    return line, ax 


if __name__ == "__main__": 
    fig, ax = plt.subplots() 
    anim = FuncAnimation(fig, update, frames=np.arange(0,10), interval=200) 
    anim.save('Test.gif', dpi=80, writer='imagemagick') 

をそして、私は私のスクリプトを実行しようとすると、私は次のエラーを取得する:次のように自分のコードの

私の適応は行く 名エラー:名「更新」に定義されていません。

私が以前に言ったように、私はまだアニメーション化する方法を学んでおり、私が見つけたコードチュートリアルで何が起こっているのかをすべて理解していません。しかし、なぜ更新が全く認識されないのか、私が更新と呼ぶ方法はチュートリアルのものとまったく同じであるように思われるので、私は非常に混乱しています。

+0

そこには(ssPlotのような)未定義の名前がたくさんあります。 – tacaswell

+0

@tcaswell私は自分の質問を混乱させたくないので、私の定義した関数をすべて投稿しなかったが、ssPlotのようなものは既に私のために定義されていて、これまでうまく働いていた。これは現在、名前エラーを返す更新のみです。 – ICantHandleThis

+0

あなたの質問をあなたが尋ねている問題を示すコードに減らすことはできますか? – tacaswell

答えて

0
import numpy as np 
import matplotlib as mpl 
mpl.use('Agg') 
import matplotlib.pyplot as plt 
from matplotlib.animation import FuncAnimation 


def update(i, ln): 
    i = i+1 
    x = i 
    y = i ** 2 
    x_data = ln.get_xdata() 
    y_data = ln.get_ydata() 
    ln.set_data(np.concatenate(([x], x_data)), 
       np.concatenate(([y], y_data))) 
    return ln 


if __name__ == "__main__": 
    fig, ax = plt.subplots() 
    ax.set_xlim(1, 10) 
    ax.set_ylim(1, 100) 
    line, = ax.loglog([1], [1]) 
    anim = FuncAnimation(fig, update, frames=np.arange(0, 10), interval=200, 
         fargs=(line,)) 
    anim.save('Test.gif', dpi=80, writer='imagemagick') 

期待どおりに動作します。これは、あなたのコードにマスクされている他のエラーがあると私に思い出させます。

+0

チュートリアルの例を正常に作成しましたが、実際のコードをアニメーション化するのに問題があります。私は自分のコードに戻り、ステップごとにリバースエンジニアリングを試みます。しかし、私の問題を調べてくれてありがとう。 – ICantHandleThis

関連する問題