2016-08-10 3 views
0

ich diesen Code schrieb ein dynamisches Diagramm mit matplotlib zum Plotten, aber es funktioniert nicht:matplotlib Animation funktioniert nicht

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) 

die data.txt Daten sind wie diese:

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

Ich benutze Python2.7 und die neueste Version von Matplotlib

+2

Bitte achten Sie auf Einrückung, da dies (in Python) Teil der Semantik Ihres Programms ist - es könnte der Fall sein, dass darin Ihr Problem liegt – DAXaholic

+0

Ihr Beispiel funktioniert nicht. Nach 'def animate (i): sollte ein Einzug sein.'. Könnten Sie das bitte korrigieren? –

+0

im Programm ist es in Ordnung, hier, als ich es hier einfügen war es so – pouya

Antwort

0

Sie müssen ax1.plot innerhalb animieren haben.

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) 

Wenn so geschrieben, funktioniert es auf meinem Rechner.

+0

es ist da, ich finde heraus, dass das Problem mit der Version von Matplotlib ist, unterstützt nicht bestimmte GTK3-Szenarien – pouya

+0

Ich habe 1.4.2 und es funktioniert für ich, Python 2.7 –