2011-06-17 10 views
2

私はmatplotlibのサンプルページからこのコードを取り除いています。私は与えられたウィンドウを維持するためにx軸を取得しようとしています。例えば、キャンバスはx = 0から30、1から31,2から32までプロットされます。今、私のxは大きくなります。私は設定されたウィンドウサイズを定義することはできませんでした。誰でも正しい方向に向けることができます。matplotlibのライブプロット(シリアルデータストリーム)のx軸の範囲を調整する

私の試行から、xの値がどんな値であっても、yは同じ長さである必要があります。私のプログラムでは、データのシリアルストリームをプロットしたいだけです。私はこのルートに行くのか?

import time 
import matplotlib 
matplotlib.use('TkAgg') # do this before importing pylab 
import matplotlib.pyplot as plt 
import random 

fig = plt.figure() 
ax = fig.add_subplot(111) 

y = [] 

def animate(): 

    while(1): 
     data = random.random() 
     y.append(data) 
     x = range(len(y)) 

     line, = ax.plot(y) 
     line.set_ydata(y) 
     fig.canvas.draw() 

win = fig.canvas.manager.window 
fig.canvas.manager.window.after(100, animate) 
plt.show() 

答えて

0

これは、yの最後の30ポイントを表示する単純なバージョン、(あなたがそれを格納する必要はありませんように聞こえるので、実際にはそれだけで、最後の30点を除いて、すべてのデータを破棄)ですが、 X軸ラベルは、あなたが望むものと推測されていない、永遠0-30に泊まる:

def animate(y, x_window): 
    while(1): 
     data = random.random() 
     y.append(data) 
     if len(y)>x_window: 
      y = y[-x_window:] 
     x = range(len(y)) 
     ax.clear() 
     line, = ax.plot(y) 
     line.set_ydata(y) 
     fig.canvas.draw() 

fig = plt.figure() 
ax = fig.add_subplot(111) 
y = [] 
win = fig.canvas.manager.window 
fig.canvas.manager.window.after(100, animate(y,30)) 

だから私たちは断たきたどのくらいのyのを追跡するためにオフセット変数を追加し、ちょうどその数を追加しますset_xticklabelsですべてのx軸ラベルに適用します。

def animate(y, x_window): 
    offset = 0 
    while(1): 
     data = random.random() 
     y.append(data) 
     if len(y)>x_window: 
      offset += 1 
      y = y[-x_window:] 
     x = range(len(y)) 
     ax.clear() 
     line, = ax.plot(y) 
     line.set_ydata(y) 
     ax.set_xticklabels(ax.get_xticks()+offset) 
     fig.canvas.draw() 

fig = plt.figure() 
ax = fig.add_subplot(111) 
y = [] 
win = fig.canvas.manager.window 
fig.canvas.manager.window.after(100, animate(y,30)) 

これは機能しますか?

1

また、xとyの両方のデータを変更して、プロット制限を更新することもできます。私はこれをどれくらいの期間実行するつもりか分かりませんが、ある時点で不要なyデータをダンプすることを検討するべきでしょう。

import matplotlib 
matplotlib.use('TkAgg') # do this before importing pylab 
import matplotlib.pyplot as plt 
import random 

fig = plt.figure() 
ax = fig.add_subplot(111) 

x = range(30) 
y = [random.random() for i in x] 
line, = ax.plot(x,y) 

def animate(*args): 
    n = len(y) 
    for 1: 
     data = random.random() 
     y.append(data) 

     n += 1 
     line.set_data(range(n-30, n), y[-30:]) 
     ax.set_xlim(n-31, n-1) 
     fig.canvas.draw() 

fig.canvas.manager.window.after(100, animate) 
plt.show() 
+0

これは私の最終製品が欲しいものにぴったりです。私はこれを一度に約7時間走らせようとしており、毎時間何か何かを捨てるだろう。あなたの援助に感謝します。歓声! – brn2bfre

+0

速い番号を生成していますか?より頻繁にやりたいことがあります。 – matt

関連する問題