-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotting.py
60 lines (46 loc) · 1.22 KB
/
plotting.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
import matplotlib.pyplot as plt
import random
graphs = []
def set_ylabel(label):
plt.ylabel(label)
def replot():
plt.clf()
for graph in graphs:
args, kwargs = graph.get_plot_args()
plt.plot(*args, **kwargs)
plt.legend()
plt.pause(0.01)
class Graph(object):
def __init__(self, label='', points=1000):
self.label = label
self.points = points
graphs.append(self)
self.ys = []
self.xs = []
def maybe_add(self, i, x, y, plot=False):
if len(self.xs) == self.points:
r = random.randint(0 + 1, i)
if r < self.points:
self.pop(r)
self.add(x, y)
self.maybe_plot(plot)
else:
self.add(x, y)
self.maybe_plot(plot)
def pop(self, i):
self.xs.pop(i)
self.ys.pop(i)
def add(self, x, y):
self.xs.append(x)
self.ys.append(y)
def get_plot_args(self):
return (self.xs, self.ys), {'label': self.label}
def clear(self, plot=False):
self.xs.clear()
self.ys.clear()
self.maybe_plot(plot)
@staticmethod
def maybe_plot(plot):
if plot:
replot()
plt.ion()