-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplotter.py
143 lines (114 loc) · 3.9 KB
/
plotter.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import time, os
from ipc import ipc
def remote_plotter_callback(conn):
import matplotlib
import time
import threading as th
from sys import platform as _platform
if _platform == "darwin":
# MAC OS X
matplotlib.use('qt5Agg')
# avoid using cocoa backend, avoid using framework build.
# conda install pyqt
import matplotlib.pyplot as plt
class plotter:
def __init__(self,num_lines=1):
self.lock = th.Lock()
self.x = []
self.y = []
self.num_lines = num_lines
self.ys = [[] for i in range(num_lines)]
self.colors = [
[
(i*i*7.9+i*19/2.3+17/3.1)%0.5+0.2,
(i*i*9.1+i*23/2.9+31/3.7)%0.5+0.2,
(i*i*11.3+i*29/3.1+37/4.1)%0.5+0.2,
]
for i in range(num_lines)]
self.time = time.time()
self.fig = plt.figure()
self.ax = self.fig.add_subplot(1,1,1)
plt.show(block=False)
# plt.show()
self.something_new = True
def show(self):
self.lock.acquire()
if self.anything_new():
self.ax.clear()
self.ax.grid(color='#f0f0f0', linestyle='solid', linewidth=1)
x = self.x
for idx in range(len(self.ys)):
y = self.ys[idx]
c = self.colors[idx]
self.ax.plot(x,y,color=tuple(c))
for idx in range(len(self.ys)):
y = self.ys[idx]
c = self.colors[idx]
init = 5
if len(y)>init:
ysmooth = [sum(y[0:init])/init]*init
for i in range(init,len(y)): # first order
ysmooth.append(ysmooth[-1]*0.9+y[i]*0.1)
for i in range(init,len(y)): # second order
ysmooth[i] = ysmooth[i-1]*0.9+ysmooth[i]*0.1
self.ax.plot(x,ysmooth,lw=2,color=tuple([cp**0.3 for cp in c]),alpha=0.5)
self.lock.release()
plt.pause(0.2)
# plt.draw()
def pushy(self,y):
self.lock.acquire()
self.y.append(y)
if len(self.x)>0:
self.x.append(self.x[-1]+1)
else:
self.x.append(0)
self.something_new = True
self.lock.release()
def pushys(self,ys):
self.lock.acquire()
for idx in range(self.num_lines):
self.ys[idx].append(ys[idx])
if len(self.x)>0:
self.x.append(self.x[-1]+1)
else:
self.x.append(0)
self.something_new = True
self.lock.release()
def anything_new(self):
# self.lock.acquire()
n = self.something_new
self.something_new = False
# self.lock.release()
return n
p = None
endflag = False
# wait for init parameters
while 1:
msg = conn.recv()
if p is None:
if msg[0] == 'init':
p = plotter(msg[1])
break
def receive_loop():
while 1:
msg = conn.recv()
if msg[0] == 'pushys':
p.pushys(msg[1])
else:
return
th.Thread(target = receive_loop, daemon = True).start()
while 1:
p.show()
class interprocess_plotter(ipc):
def __init__(self,num_lines=1):
super().__init__(remote_plotter_callback)
self.send(('init',num_lines))
def pushys(self,ys):
self.send(('pushys', ys))
if __name__=='__main__':
ip = interprocess_plotter(2)
import math,time
for i in range(100):
ip.pushys([math.sin(i/10), math.sin(i/10+2)])
time.sleep(0.05)
time.sleep(5)