2017-10-07 6 views
0

出力する関数がreturn (x, y)で、x、yペアを最初から最後までアニメーション化したいと思います。例えば。その線が「時間の経過と共に発展する」ようにする。Matplotlib:アニメーション関数の出力をステップで

x, y = stephan() 
plt.plot(x,y) 

enter image description here

そして、私はアニメーションコードのsnippitを使用しようとすると::

これは私の出力は次のようになります

fig = plt.figure() 
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2)) 

line, = ax.plot([], []) 

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

def animate(i): 
    x, y = stephan() 
    line.set_data(x, y[i]) 
    return line, 

anim = animation.FuncAnimation(fig, animate, init_func=init, 
           frames=100, interval=20, blit=True) 

私はこれはかなり退屈な出力が得られます。

enter image description here

何かをプロットしていますが、確かにx、y出力ではありません。アニメートやinit関数を間違って使っているかもしれないと思いますか?不思議なことに、私はこれを非常に簡単に行うコードを見つけることができませんでした。

+0

問題の[mcve]を必ず入力してください。ここでは、「x」と「y」が何であるか知りません。それらは浮動小数点数、リスト、配列、文​​字列ですか?また、 'line.set_data(x、y [i])'は何をしているのでしょうか? – ImportanceOfBeingErnest

答えて

0

stephanへの呼び出しが2つの浮動小数点数を返す場合、y[i]は意味をなさない。さらに、出力をリストに格納して、フレームごとに1つのドットを取得することもできます。 stephan戻り値の二つのリストは、あなたが直接ラインにデータとしてこれらのリストを設定することができれば

A実施例では、代わりにこの

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

class Stephan(): 
    i=0 
    def __call__(self): 
     self.i += 0.02 
     return self.i, np.random.randn(1) 

stephan = Stephan() 

fig = plt.figure() 
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2)) 

line, = ax.plot([], []) 

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

X = [] 
def animate(i): 
    x, y = stephan() 
    X.append((x,y)) 
    line.set_data(zip(*X)) 
    return line, 

anim = animation.FuncAnimation(fig, animate, init_func=init, 
           frames=100, interval=20, blit=True) 

plt.show() 

ようになります。

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

class Stephan(): 
    x=[0] 
    y=[1] 
    def __call__(self): 
     self.x.append(self.x[-1]+0.02) 
     self.y.append(np.random.randn(1)) 
     return self.x, self.y 

stephan = Stephan() 

fig = plt.figure() 
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2)) 

line, = ax.plot([], []) 

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

def animate(i): 
    x, y = stephan() 
    line.set_data(x,y) 
    return line, 

anim = animation.FuncAnimation(fig, animate, init_func=init, 
           frames=100, interval=20, blit=True) 

plt.show() 
関連する問題