Skip to content

Commit d92a19d

Browse files
committed
Adding matplotlib linegraph example.
1 parent aa56c8f commit d92a19d

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

plot_emgs_mat.py

+50-24
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import multiprocessing
2+
import queue
3+
import numpy as np
24
import mpl_toolkits.mplot3d as plt3d
35
from mpl_toolkits.mplot3d import Axes3D
46
import matplotlib.pyplot as plt
57
from matplotlib import animation
6-
import numpy as np
8+
from matplotlib.cm import get_cmap
79

810
from myo_serial import MyoRaw
911

10-
print("EPILEPSY WARNING")
11-
print("The plot updates fast, press ctrl+c to stop")
12+
print("Press ctrl+pause/break to stop")
1213

1314
# ------------ Myo Setup ---------------
1415
q = multiprocessing.Queue()
@@ -34,41 +35,66 @@ def print_battery(bat):
3435
while True:
3536
try:
3637
m.run()
37-
except KeyboardInterrupt:
38+
except:
3839
print("Worker Stopped")
3940
quit()
4041

41-
4242
# ------------ Plot Setup ---------------
43-
fig = plt.figure()
44-
axm = fig.add_subplot()
45-
46-
myox = [2048,128,128,128,128,128,128,128]
47-
rects = axm.bar(list(range(1,9)), myox)
43+
QUEUE_SIZE = 100
44+
SENSORS = 8
45+
subplots = []
46+
lines = []
47+
# Set the size of the plot
48+
plt.rcParams["figure.figsize"] = (4,8)
49+
# using the variable axs for multiple Axes
50+
fig, subplots = plt.subplots(SENSORS, 1)
51+
fig.tight_layout()
52+
# Set each line to a different color
53+
54+
name = "tab10" # Change this if you have sensors > 10
55+
cmap = get_cmap(name) # type: matplotlib.colors.ListedColormap
56+
colors = cmap.colors # type: list
57+
58+
for i in range(0,SENSORS):
59+
ch_line, = subplots[i].plot(range(QUEUE_SIZE),[0]*(QUEUE_SIZE), color=colors[i])
60+
lines.append(ch_line)
61+
62+
emg_queue = queue.Queue(QUEUE_SIZE)
4863

4964
def animate(i):
50-
myox = [128,128,128,128,128,128,128,128]
51-
5265
# Myo Plot
5366
while not(q.empty()):
5467
myox = list(q.get())
55-
print(myox)
56-
57-
for rect, yi in zip(rects, myox):
58-
rect.set_height(yi)
68+
if (emg_queue.full()):
69+
emg_queue.get()
70+
emg_queue.put(myox)
5971

60-
return rects
72+
channels = np.array(emg_queue.queue)
6173

62-
def main():
63-
anim = animation.FuncAnimation(fig, animate, blit=False, interval=2)
64-
try:
65-
plt.show()
66-
except KeyboardInterrupt:
67-
quit()
74+
if (emg_queue.full()):
75+
for i in range(0,SENSORS):
76+
channel = channels[:,i]
77+
lines[i].set_ydata(channel)
78+
subplots[i].set_ylim(0,max(1024,max(channel)))
6879

6980
if __name__ == '__main__':
7081
# Start Myo Process
7182
p = multiprocessing.Process(target=worker, args=(q,))
7283
p.start()
7384

74-
main()
85+
while(q.empty()):
86+
# Wait until we actually get data
87+
continue
88+
anim = animation.FuncAnimation(fig, animate, blit=False, interval=2)
89+
def on_close(event):
90+
p.terminate()
91+
raise KeyboardInterrupt
92+
print("On close has ran")
93+
fig.canvas.mpl_connect('close_event', on_close)
94+
95+
try:
96+
plt.show()
97+
except KeyboardInterrupt:
98+
plt.close()
99+
p.close()
100+
quit()

0 commit comments

Comments
 (0)