-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlot.py
More file actions
executable file
·55 lines (44 loc) · 1.59 KB
/
Plot.py
File metadata and controls
executable file
·55 lines (44 loc) · 1.59 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
#
# Plot.py
# Aidlab-SDK
# Created by Szymon Gesicki on 10.05.2020.
#
import numpy as np
import time
import matplotlib.pyplot as pyplot
import platform
if platform.system() == 'Darwin':
# For older mac versions
if int(platform.release().split('.')[0]) <= 20:
import matplotlib
matplotlib.use('TkAgg')
buffer_size = 750
class Plot:
def __init__(self):
self.y = [0] * buffer_size
self.line = []
self.time = time.time() * 1000
self.sample_index = 0
def live_plotter(self):
if self.line == []:
# This is the call to matplotlib that allows dynamic plotting
pyplot.ion()
self.fig = pyplot.figure(figsize=(13, 6))
axis = self.fig.add_subplot(111)
# Create a variable for the line so we can later update it
self.line, = axis.plot(self.y, '', alpha=0.8)
pyplot.show()
# After the figure, axis, and line are created, we only need to update the
# y-data
self.line.set_ydata(self.y)
# Adjust limits if new data goes beyond bounds
pyplot.ylim([np.min(self.y) - np.std(self.y), np.max(self.y) + np.std(self.y)])
# This pauses the data so the figure/axis can catch up - the amount of pause
# can be altered above
self.fig.canvas.flush_events()
def add(self, value):
self.y = self.y[1:] + [value] # shift values in the buffer and append new one
self.sample_index += 1
if time.time() * 1000 - self.time > 200:
self.time = time.time() * 1000
self.live_plotter()