-
Notifications
You must be signed in to change notification settings - Fork 3
/
plotData.py
72 lines (51 loc) · 1.89 KB
/
plotData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#Code to present data created by TFlow app graphically
#Created in 24/06/2015 by Eduardo Henrique Arnold
#Credits to Danial Taherzadeh (https://taher-zadeh.com/speeding-matplotlib-plotting-times-real-time-monitoring-purposes/) for the dynamic update of Matplotlib, significantly improving performance to achieve real time results
import re
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
def getDataFromString(s):
res = re.match('(\d+\.*\d*)ms occ=(\d+\.*\d*) flow=(\d+\.*\d*)', s)
if res:
vec = res.group(1,2,3) #tuple of results in string form
vec = [float(v) for v in vec] #convert them to float
return vec
#Use style
plt.style.use('ggplot')
#Remove toolbar
plt.rcParams['toolbar'] = 'None'
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_title("Traffic Parameters vs Time")
ax.hold(True)
maxCountSpan = 100
data = np.zeros((maxCountSpan, 3))
pOcc, = plt.plot(data[:,0], data[:,1], color='r', label='Occupancy')
pFlw, = plt.plot(data[:,0], data[:,2], color='b', label='Flow')
ax.legend(loc='upper right')
fig.show(False)
fig.canvas.draw()
#Cache bgnd
background = fig.canvas.copy_from_bbox(ax.bbox)
while True:
ln = raw_input()
newData=getDataFromString(ln)
#roll array
data = np.roll(data, -1, 0)
#copy newData to end of data
data[-1,:] = newData
#Update plot data
pOcc.set_data(data[:,0], data[:,1])
pFlw.set_data(data[:,0], data[:,2])
#Adjust axis to new time position
plt.axis([data[0,0], data[-1,0], 0, 0.8])
#Reset bgnd
fig.canvas.restore_region(background)
#Redraw just the lines
ax.draw_artist(pOcc)
ax.draw_artist(pFlw)
#Fill axes
fig.canvas.blit(ax.bbox)