2016-08-10 8 views
0

動作しない私はmatplotlibのを使って動的なダイアグラムをプロットするために、このコードを書いたが、それは動作しません:matplotlibのアニメーションが

from matplotlib import style 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
fig = plt.figure() 
ax1 = fig.add_subplot(1,1,1) 
def animate(i): 
graph_data = open ('data.txt','r').read() 
lines = graph_data.split('\n') 
xs = [] 
ys = [] 
for line in lines: 
    if len(line) > 1: 
     x, y = line.split(',') 
     # print(x,y) 
     xs.append(x) 
     ys.append(y) 
# print(xs,ys) 
# ax1.clear() 
ax1.plot(xs, ys) 
ani = animation.FuncAnimation(fig, animate, interval=1000) 

data.txtを件のデータがこれらのようなものです:

1,0.8 
2,1.4 
3,1.4 
4,2.6 
5,1.5 
6,1.6 
7,2.4 

私はpython2.7とmatplotlibの最新バージョンを使用します

+2

これは(Pythonで)プログラムのセマンティクスの一部であるため、字下げに注意してください - あなたの問題がある場合があります – DAXaholic

+0

あなたの例は機能していません。 'def animate(i):'の後にインデントがあるはずです。これを修正してください。 –

+0

がプログラムのokです。ここに貼り付けたときには – pouya

答えて

0

アニメーションの中にはax1.plotが必要です。

def animate(i): 
    graph_data = open ('data.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.plot(xs, ys) 

これは私のマシンで動作します。

+0

があります。問題はmatplotlibのバージョンであり、特定のGTK3シナリオをサポートしていません。 – pouya

+0

私は1.4.2を持っています。私、Python 2.7 –

関連する問題