2017-05-16 15 views
0

私は10秒ごとに更新されるログファイルを持っています。これは私がリアルタイム描画にはPython 2.7でmatplotlibを使用する30秒matplotlibを使ってx-tickでリアルタイム更新範囲を設定する方法は?

Iteration 0, data = 1.0 
Iteration 10, data = 4.0 
Iteration 20, data = 5.0 
Iteration 30, data = 8.0 

でのログの内容です。軽微な問題なしでうまくいきました。問題は、x-tick(反復数を表す)の値が前の範囲をクリアしないため、x-tickで重なって表示される範囲になります(同じ問題はy-tickで表示されます)。これは、log.txtをもう一つの行にIteration 40, data = 5.0と追加すると、xティックの値は古いイテレーションの間で重複することを意味します。[0,10,20,30][0,10,20,30,40]。最新の範囲[0,10,20,30,40]を表示したいだけです。どうすれば修正できますか?

enter image description here

これは、私はあなたがここに2つのオプションがあると思う私のコード

import matplotlib.pyplot as plt 
import re 
from mpl_toolkits.axes_grid1 import host_subplot 

plt.ion() 
while True: 
    f = open("./log.txt", 'r') 
    iterations = [] 
    data = [] 
    for line in f: 
     if 'Iteration ' in line and 'data = ' in line: 
      arr = re.findall(r'\b\d+\b,', line) 
      iterations.append(int(arr[0].strip(',')[0:])) 
      data.append(float(line.strip().split(' ')[-1])) 
    f.close() 

    host = host_subplot(111) # , axes_class=AA.Axes) 
    plt.subplots_adjust(right=0.75) 
    host.set_xlabel("Iterations") 
    host.set_ylabel("Loss") 
    p1, = host.plot(iterations, data, label="Data") 
    plt.pause(3) 

答えて

1

です。いずれにしても、ループ外に軸を作成する必要があります。そうしないと、オーバーレイする軸がたくさんあります。

  1. クリア軸

    import matplotlib.pyplot as plt 
    import re 
    from mpl_toolkits.axes_grid1 import host_subplot 
    
    plt.ion() 
    host = host_subplot(111) 
    plt.subplots_adjust(right=0.75) 
    
    while True: 
        f = open("./log.txt", 'r') 
        iterations = [] 
        data = [] 
        for line in f: 
         if 'Iteration ' in line and 'data = ' in line: 
          arr = re.findall(r'\b\d+\b,', line) 
          iterations.append(int(arr[0].strip(',')[0:])) 
          data.append(float(line.strip().split(' ')[-1])) 
        f.close() 
    
        host.clear() 
        host.set_xlabel("Iterations") 
        host.set_ylabel("Loss") 
        p1, = host.plot(iterations, data, label="Data") 
        plt.draw() 
        plt.pause(3) 
    
  2. 更新データ

    import matplotlib.pyplot as plt 
    import re 
    from mpl_toolkits.axes_grid1 import host_subplot 
    
    plt.ion() 
    
    host = host_subplot(111) 
    plt.subplots_adjust(right=0.75) 
    host.set_xlabel("Iterations") 
    host.set_ylabel("Loss") 
    p1, = host.plot([],[], label="Data") 
    
    while True: 
        f = open("./log.txt", 'r') 
        iterations = [] 
        data = [] 
        for line in f: 
         if 'Iteration ' in line and 'data = ' in line: 
          arr = re.findall(r'\b\d+\b,', line) 
          iterations.append(int(arr[0].strip(',')[0:])) 
          data.append(float(line.strip().split(' ')[-1])) 
        f.close() 
        p1.set_data(iterations, data) 
        host.relim() 
        host.autoscale_view() 
        plt.draw() 
        plt.pause(3) 
    
+0

私は最初のソリューションを試してみましたが、それがうまく働きました。 – KimHee

+0

凡例を使用するとどうですか? 'host.legend(loc = 2)host.axis [" left "]。label.set_color(p1.get_color())'。どのように私はどこに移動する必要がありますか? – KimHee

+0

あなたが何を求めているのか分かりません。伝説はありませんが、伝説を使用したい場合は、伝説に関する多くの質問があります。 – ImportanceOfBeingErnest

関連する問題