-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRealTimePlotter.py
54 lines (49 loc) · 2.08 KB
/
RealTimePlotter.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
import serial
import numpy as np
from matplotlib import pyplot as plt
import time
from threading import Thread,Event,Lock
class RealTimePlotter:
def __init__(self):
plt.ion()
self.fig, (self.time_axis,self.freq_axis) = plt.subplots(2)
self.fig.suptitle('Real-time updated data', fontsize='14', fontweight='bold')
self.time_axis.set_title('time data', fontsize='14', fontweight='bold')
self.time_axis.set_xlabel('time, seconds', fontsize='11', fontstyle='italic')
self.time_axis.set_ylabel('amplitude, V', fontsize='11', fontstyle='italic')
self.freq_axis.set_title('frequency data', fontsize='14', fontweight='bold')
self.freq_axis.set_xlabel('frequency, Hz', fontsize='11', fontstyle='italic')
self.freq_axis.set_ylabel('amplitude, log', fontsize='11', fontstyle='italic')
self.fig.subplots_adjust(hspace=0.5)
def show(self):
try:
x = [0]
y = [2.6,-2.6]
self.time_line, = self.time_axis.plot(y, y)
self.freq_line, = self.freq_axis.plot(x, x)
self.fig.canvas.draw()
self.fig.show()
plt.show(block=False)
except KeyboardInterrupt:
self.fig.close('all')
def plot(self,buffer,stats):
try:
fft = np.log(np.abs(np.fft.rfft(buffer))+1e-8)
t = np.linspace(0,stats['elapsed_time'],buffer.shape[0])
f = np.linspace(0,stats['fs'],fft.shape[0])
#self.time_axis.clear()
#self.freq_axis.clear()
self.time_line.set_xdata(t)
self.time_line.set_ydata(buffer)
self.freq_line.set_xdata(f)
self.freq_line.set_ydata(fft)
self.time_axis.relim()
self.time_axis.autoscale_view(True,True,False)
self.freq_axis.relim()
self.freq_axis.autoscale_view(True,True,True)
#self.time_axis.plot(t,buffer)
#self.freq_axis.plot(f,fft)
self.fig.canvas.draw()
plt.pause(0.001)
except Exception as e:
print(e)