-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtorcs_summary_plotter.py
More file actions
84 lines (72 loc) · 2.63 KB
/
torcs_summary_plotter.py
File metadata and controls
84 lines (72 loc) · 2.63 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
class AnimatedScatter(object):
"""An animated scatter plot using matplotlib.animations.FuncAnimation."""
def __init__(self):
self.stream = self.data_stream()
self.f = open('/home/christopher/data_bin/summary_car_data.txt')
# Setup the figure and axes...
self.fig, self.ax = plt.subplots()
# Then setup FuncAnimation.
self.ani = animation.FuncAnimation(self.fig, self.update, interval=5,
init_func=self.setup_plot, blit=True)
def setup_plot(self):
"""Initial drawing of the scatter plot."""
dat = next(self.stream)
x = dat[:, 0]
y = dat[:, 1]
s = dat[:, 2]
c = dat[:, 3]
self.scat = self.ax.scatter(x, y, c=c, s=s, animated=True)
# self.ax.axis([0, 100, 0, 100])
# For FuncAnimation's sake, we need to return the artist we'll be using
# Note that it expects a sequence of artists, thus the trailing comma.
return self.scat,
def get_next_data_point(self):
cur_row = []
while True:
line = self.f.readline()
if line == '':
continue
if line == '.\n':
return cur_row
else:
split = line.split()
value = float(split[-1])
cur_row.append(value)
def data_stream(self):
"""Generate a random walk (brownian motion). Data is scaled to produce
a soft "flickering" effect."""
data = np.zeros((1,4))
# x = data[:, 0]
# y = data[:, 1]
# s = data[:, 2]
# c = data[:, 3]
count = 0
while True:
cur_row = self.get_next_data_point()
data[count] = cur_row[:4]
count += 1
cur_size = len(data)
if count >= cur_size:
new_rows = np.zeros((cur_size,4))
data = np.vstack([data, new_rows])
yield data
def update(self, i):
"""Update the scatter plot."""
data = next(self.stream)
# Set x and y data...
self.scat.set_offsets(data[:,:2])
# Set sizes...
self.scat._sizes = (0.1 * abs(data[:,2]))**1.5 + 10
# Set colors..
self.scat.set_array(data[:,3])
# We need to return the updated artist for FuncAnimation to draw..
# Note that it expects a sequence of artists, thus the trailing comma.
return self.scat,
def show(self):
plt.show()
if __name__ == '__main__':
a = AnimatedScatter()
a.show()