在实时图中创建多个不同的行

时间:2017-02-10 13:24:39

标签: python python-3.x matplotlib

我在python中的新手,我想创建实时图表来分析我的数据。我将从我的本地文件中获取数据,我需要为每个数据创建多行。

所以我想在图上看到多行

示例本地数据(一行):

1,4
2,5
3,7
4,9
5,2
6,8
7,1
8,3
9,10
10,4

代码:

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

import numpy as np
import urllib
import datetime as dt


style.use('fivethirtyeight')

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



def animate(i):
    graph_data=open('api2.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)

ax1.plot([],[],linewidth=5, label='loss', color='r', alpha=0.5)


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

sample output

希望使用我的原始数据样本(多行就是这样):

142.5,423.0,225.2,423.5,942.1,541.5
518.6,234.1,115.9,163.5,412.0,234.8
123.4,144.6,435.8,341.1,623.8,423.1
454.2,734.4,645.7,633.7,412.5,991.3
441.4,414.1,413.6,912.1,674.3,233.6
712.2,242.4,865.5,523.9,122.2,133.4
142.5,423.0,225.2,423.5,942.1,541.5
518.6,234.1,115.9,163.5,412.0,234.8
123.4,144.6,435.8,341.1,623.8,423.1
454.2,734.4,645.7,633.7,412.5,991.3
441.4,414.1,413.6,912.1,674.3,233.6
712.2,242.4,865.5,523.9,122.2,133.4

0 个答案:

没有答案