2017-07-14 13 views
3

オンライン検索の助けを借りて次のコードを書いています。ここに私の意図は、私はちょうど、Y軸の範囲が連続的に変化する参照x軸上の時間と上記のコードではy軸matplotlibを使ってリアルタイムグラフをプロットできません

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

fig = plt.figure() 
ax1 = fig.add_subplot(1,1,1) 

def animate(i): 
    xar = [] 
    yar = [] 
    x,y = time.time(), np.random.rand() 
    xar.append(x) 
    yar.append(y) 
    ax1.clear() 
    ax1.plot(xar,yar) 
ani = animation.FuncAnimation(fig, animate, interval=1000) 
plt.show() 

上のいくつかのランダムに生成された値をリアルタイムでグラフを取得することで、グラフが表示されません。図では、

答えて

1

問題は、xvaryvarを更新しないことです。リストの定義をanimateの定義外に移動することで、これを行うことができます。

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

fig = plt.figure() 
ax1 = fig.add_subplot(1,1,1) 
xar = [] 
yar = [] 

def animate(i): 
    x,y = time.time(), np.random.rand() 
    xar.append(x) 
    yar.append(y) 
    ax1.clear() 
    ax1.plot(xar,yar) 
ani = animation.FuncAnimation(fig, animate, interval=1000) 
plt.show() 
関連する問題