Как построить живой график - PullRequest
0 голосов
/ 08 марта 2019

Я пишу программу для чтения данных из текстового файла:

[2.8389999866485596, 2.8459999561309814, inf, 0.3540000021457672, 0.3070000112056732, 0.28700000047683716, 0.296999990940094, 0.29600000381469727]

Для этого графика нет оси y, в настоящее время это код, который я написал:

import matplotlib.pyplot as plt
import matplotlib.animation as animation

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

def animate(i):
    f = open('sample_data.txt', 'r').read()
    lines = f.split('\n')
    xs=[] 
    ys=[]

    for line in lines:
        if len(line) > 1:
            x,y = line.split(',')
            xs.append(float(x))
            ys.append(float(y))

    ax1.clear()
    ax1.plot(xs,ys)

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

Но есть много ошибок.

1 Ответ

0 голосов
/ 08 марта 2019

Можете ли вы попробовать следующее:

def animate(i):
    f = open('sample_data.txt', 'r').read().replace('\n', '')
    ys = list(map(float, f.split(',')))
    # lines = f.split('\n')
    # xs=[]
    # ys=[]
    # for line in lines:
        # if len(line) > 1:
            # x,y = line.split(',')
            # xs.append(float(x))
            # ys.append(float(y))
    ax1.clear()
    ax1.plot(ys)

Чтобы получить ranges из файла txt:

fp = r"python .txt"
with open(fp, 'r') as infile:
    data = infile.read()

for val in data.split('\n'):
    if val.startswith('ranges'):
        print(list(map(lambda x: x.strip(), val.split(':')[1].strip().replace('[', '').replace(']', '').split(','))))
        print()

Он напечатает весь текст внутри []из каждой строки, которая начинается с ranges.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...