2017-12-31 51 views
-1

Matplotlib (following this exact tutorial)을 사용하여 파이썬으로 실시간 그래프를 작성하려고합니다. 하지만 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