-
Notifications
You must be signed in to change notification settings - Fork 0
/
MutliprocessPlot.py
162 lines (123 loc) · 6.09 KB
/
MutliprocessPlot.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import matplotlib.pyplot as plt
import numpy as np
import random
import time
from multiprocessing import Pipe, Process, Value
import matplotlib
matplotlib.use('Qt5Agg')
class MutliprocessPlot():
def __init__(self, conn, drawSize = 100, bound = [-100, 200]) -> None:
self.conn = conn
self.drawSize = drawSize
self.bound = bound
self.isDraw = Value('i', 0)
self.p = Process(target=self.DrawPic, args=(self.isDraw, ))
self.p.daemon = True
def initFig(self):
self.fig = plt.figure(figsize=(13, 6))
self.ax1 = self.fig.add_subplot(221)
self.ax2 = self.fig.add_subplot(222)
self.ax3 = self.fig.add_subplot(223)
self.ax4 = self.fig.add_subplot(224)
self.ax1.set_ylim(self.bound)
self.ax1.set_xlim([0, self.drawSize])
self.ax2.set_ylim(self.bound)
self.ax2.set_xlim([0, self.drawSize])
self.ax3.set_ylim(self.bound)
self.ax3.set_xlim([0, self.drawSize])
self.ax4.set_ylim(self.bound)
self.ax4.set_xlim([0, self.drawSize])
plt.ioff()
def SetStatus(self, status):
self.isDraw.value = status
def Start(self):
self.p.start()
def DrawPic(self, isDraw):
self.initFig()
data = np.array([]).reshape(0,14)
lineMean1, = self.ax1.plot(data[-self.drawSize:, 1], 'g-', lw = 1)
lineStdUp1, = self.ax1.plot(data[-self.drawSize:, 2], 'g-', lw = 0.5)
lineStdDown1, = self.ax1.plot(data[-self.drawSize:, 0], 'g-', lw = 0.5)
lineReal1, = self.ax1.plot(data[-self.drawSize:, 1], 'r-', lw = 1)
lineMean2, = self.ax2.plot(data[-self.drawSize:, 1])
lineStdUp2, = self.ax2.plot(data[-self.drawSize:, 2])
lineStdDown2, = self.ax2.plot(data[-self.drawSize:, 0])
lineReal2, = self.ax2.plot(data[-self.drawSize:, 1])
lineMean3, = self.ax3.plot(data[-self.drawSize:, 1])
lineStdUp3, = self.ax3.plot(data[-self.drawSize:, 2])
lineStdDown3, = self.ax3.plot(data[-self.drawSize:, 0])
lineReal3, = self.ax3.plot(data[-self.drawSize:, 1])
lineMean4, = self.ax4.plot(data[-self.drawSize:, 1])
lineStdUp4, = self.ax4.plot(data[-self.drawSize:, 2])
lineStdDown4, = self.ax4.plot(data[-self.drawSize:, 0])
lineReal4, = self.ax4.plot(data[-self.drawSize:, 1])
self.fig.canvas.draw()
axbackground1 = self.fig.canvas.copy_from_bbox(self.ax1.bbox)
axbackground2 = self.fig.canvas.copy_from_bbox(self.ax2.bbox)
axbackground3 = self.fig.canvas.copy_from_bbox(self.ax3.bbox)
axbackground4 = self.fig.canvas.copy_from_bbox(self.ax4.bbox)
plt.show(block=False)
while isDraw.value:
temp = self.conn.recv()
data = np.vstack((data, temp))
lineMean1.set_data(range(len(data[-self.drawSize:, 4])), data[-self.drawSize:, 4])
lineStdUp1.set_data(range(len(data[-self.drawSize:, 4])), data[-self.drawSize:, 4] + data[-self.drawSize:, 8])
lineStdDown1.set_data(range(len(data[-self.drawSize:, 4])), data[-self.drawSize:, 4] - data[-self.drawSize:, 8])
lineReal1.set_data(range(len(data[-self.drawSize:, 0])), data[-self.drawSize:, 0])
lineMean2.set_data(range(len(data[-self.drawSize:, 5])), data[-self.drawSize:, 5])
lineStdUp2.set_data(range(len(data[-self.drawSize:, 5])), data[-self.drawSize:, 5] + data[-self.drawSize:, 9])
lineStdDown2.set_data(range(len(data[-self.drawSize:, 5])), data[-self.drawSize:, 5] - data[-self.drawSize:, 9])
lineReal2.set_data(range(len(data[-self.drawSize:, 1])), data[-self.drawSize:, 1])
lineMean3.set_data(range(len(data[-self.drawSize:, 6])), data[-self.drawSize:, 6])
lineStdUp3.set_data(range(len(data[-self.drawSize:, 6])), data[-self.drawSize:, 6] + data[-self.drawSize:, 10])
lineStdDown3.set_data(range(len(data[-self.drawSize:, 6])), data[-self.drawSize:, 6] - data[-self.drawSize:, 10])
lineReal3.set_data(range(len(data[-self.drawSize:, 2])), data[-self.drawSize:, 2])
lineMean4.set_data(range(len(data[-self.drawSize:, 7])), data[-self.drawSize:, 7])
lineStdUp4.set_data(range(len(data[-self.drawSize:, 7])), data[-self.drawSize:, 7] + data[-self.drawSize:, 11])
lineStdDown4.set_data(range(len(data[-self.drawSize:, 7])), data[-self.drawSize:, 7] - data[-self.drawSize:, 11])
lineReal4.set_data(range(len(data[-self.drawSize:, 3])), data[-self.drawSize:, 3])
self.fig.canvas.restore_region(axbackground1)
self.fig.canvas.restore_region(axbackground2)
self.fig.canvas.restore_region(axbackground3)
self.fig.canvas.restore_region(axbackground4)
self.ax1.draw_artist(lineMean1)
self.ax1.draw_artist(lineStdUp1)
self.ax1.draw_artist(lineStdDown1)
self.ax2.draw_artist(lineMean2)
self.ax2.draw_artist(lineStdUp2)
self.ax2.draw_artist(lineStdDown2)
self.ax3.draw_artist(lineMean3)
self.ax3.draw_artist(lineStdUp3)
self.ax3.draw_artist(lineStdDown3)
self.ax4.draw_artist(lineMean4)
self.ax4.draw_artist(lineStdUp4)
self.ax4.draw_artist(lineStdDown4)
self.fig.canvas.blit(self.ax1.bbox)
self.fig.canvas.blit(self.ax2.bbox)
self.fig.canvas.blit(self.ax3.bbox)
self.fig.canvas.blit(self.ax4.bbox)
self.fig.canvas.flush_events()
# plt.pause(0.0000000000001)
# plt.draw()
def Update(conn):
std = random.randint(50, 60)
mean = random.randint(0, 100)
conn.send([mean - std, mean, mean + std])
def main():
main_conn, plot_conn = Pipe()
t = MutliprocessPlot(plot_conn)
t.SetStatus(1)
t.Start()
try:
indx = 0
while indx < 500:
indx += 1
Update(main_conn)
t.SetStatus(0)
except KeyboardInterrupt:
# t.SetStatus(0)
t.p.join()
plt.close()
print('close')
if __name__ == "__main__":
main()