2017-02-04 10 views
0

組み込みのアニメーション機能に頼らずにmatplotlibでグラフをアニメーション表示する方法はありますか?ポイントをプロットし、グラフを消去してから次のポイントをプロットする方がずっと簡単です。 アニメーション機能なしのmatplotlibでのアニメーション

は私のような何かを想像:各 tは異なるフレームだろう

def f(): 
    # do stuff here 
    return x, y, t 

私は、plt.clf()plt.close()などのようなものを試しましたが、何も動作していないようです。

答えて

2

FuncAnimationなしでアニメーションすることは可能です。しかし、 "enivisioned function"の目的は本当に明確ではありません。アニメーションでは、時間は独立変数です。つまり、各時間ステップごとにプロットするための新しいデータを作成します。したがって、関数はtを入力とし、いくつかのデータを戻します。

import matplotlib.pyplot as plt 
import numpy as np 

def f(t): 
    x=np.random.rand(1) 
    y=np.random.rand(1) 
    return x,y 

fig, ax = plt.subplots() 
ax.set_xlim(0,1) 
ax.set_ylim(0,1) 
for t in range(100): 
    x,y = f(t) 
    # optionally clear axes and reset limits 
    #plt.gca().cla() 
    #ax.set_xlim(0,1) 
    #ax.set_ylim(0,1) 
    ax.plot(x, y, marker="s") 
    ax.set_title(str(t)) 
    fig.canvas.draw() 
    plt.pause(0.1) 

plt.show() 

また、FuncAnimationを避けたい理由は明確ではありません。以下のように、上記と同様のアニメーションがFuncAnimationで製造することができる。

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

def f(t): 
    x=np.random.rand(1) 
    y=np.random.rand(1) 
    return x,y 

fig, ax = plt.subplots() 
ax.set_xlim(0,1) 
ax.set_ylim(0,1) 

def update(t): 
    x,y = f(t) 
    # optionally clear axes and reset limits 
    #plt.gca().cla() 
    #ax.set_xlim(0,1) 
    #ax.set_ylim(0,1) 
    ax.plot(x, y, marker="s") 
    ax.set_title(str(t)) 

ani = matplotlib.animation.FuncAnimation(fig, update, frames=100) 
plt.show() 

は、あなたが同じ行数、ここで見ることが本当に厄介な何を持って、多くの変更はありません。
さらに、アニメーションが複雑になるとき、アニメーションを繰り返すとき、ブリッティングを使用するとき、またはファイルにエクスポートしたいときはFuncAnimationのすべてのメリットがあります。

関連する問題