2017-12-31 76 views
-1

Matplotlib(following this exact tutorial)を使用してPythonでライブグラフを作成しようとしています。しかし、X軸の順序は、コード内で間違っています.1で始まり10になり、11に戻り、次に2,3,4,5に戻ります。X軸のMatplotlibの順序が正しくない

コードをコピーしました。チュートリアルの数字ですが、私は別の結果を得ています。ここでは、それは私のために示したグラフである:

enter image description here

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
from matplotlib import style 

style.use('fivethirtyeight') 

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

def animate(i): 

    graph_data = open("animation_file.txt", 'r').read() 
    lines = graph_data.split('\n') 
    xs = [] 
    ys = [] 

    for line in lines: 

     if len(line) > 1: 

      x, y = line.split(',') 
      xs.append(x) 
      ys.append(y) 
    ax1.clear() 
    ax1.plot(xs, ys) 

ani = animation.FuncAnimation(fig, animate, interval=1000) 
plt.show() 

そしてここでは、 "animation_file.txt" です:

1, 5 
2, 3 
3, 4 
4, 7 
5, 4 
6, 3 
7, 6 
8, 7 
9, 4 
10,4 
11, 2 
+0

あなたは、インターネット上でいくつかのコードを発見し、あなたはそれがどのように動作するかわからないのですか? Matplotlibのドキュメントを読んでいつでも過ごしましたか?あなたはコードの一部を除外しましたか?問題は何だと思われますか?それを検証するために何をしましたか? – wwii

答えて

3

あなたがすべきながら、あなたは文字列として、xとyの値を処理しています数値として解析する:

xs.append(float(x)) 
ys.append(float(y)) 

また、

x, y = map(float, line.split(',')) 

結果:

plot

関連する問題