I know this may be an old topic, I have searched through a lot of related topics on Google but haven't really found useful information. I am trying to get data from a microcontroller board and plot data in 3D in Python on computer. I used PySerial library to implement this and now I'm able to receive data from the port. The obstacle that I'm facing now is that I need to plot those points (consist of x,y,z) in "real time", which means once the graph is generated, it will stay on and keep updating with new points.
Initially I tried plt.show() but it didn't work well since it only shows the whole graph after the loop is done, then I tried to use plt.draw(), it seems a little bit strange while running because the GUI pop-out window always become "not responding" after a while.
I'm using Python 2.7.5 on Window.
Here is my code for plotting one point:
import serial
import sys
import Queue
import threading
import scipy.io
import numpy as num
from mpl_toolkits.mplot3d import *
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
ser = serial.Serial(port='COM6',timeout=10, baudrate= 57600,bytesize=serial.EIGHTBITS,)
flag = ser.isOpen()
print flag # check if it's opened
# draw the frame
x1 = [164, 94, 0, -100.5]
x2 = [164, 94, 0, -100.5]
y1= [-72.5, -103.5, -103.5, -134.5]
y2= [72.5, 103.5, 103.5, 134.5]
z1 = [112, 60, 3, 3]
z2 = [112, 60, 3, 3]
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.plot(x1, y1, z1, color = 'b')
ax.plot(x2, y2, z2, color = 'r')
plt.hold(True)
while True:
my_list = ser.read(9999) # read 9999 byte
if (ser.isOpen() == False):
break
n = ser.inWaiting() # read the number of chars in the receiver buffer,
# after testing, I found that the max value of n is 4096
if n == 0:
pass
my_list = my_list.split('+00') # split out "+00", make my_list become a list
for item in my_list:
# remove space
if item == '':
my_list.remove(item)
else:
item = float(item) # convert to float type
pass
ax = fig.gca(projection='3d') # to work in 3d
x = my_list.pop(0) # pop the first element in list to x
Floatx= float(x)
y = my_list.pop(0)
Floaty= float(y)
z = my_list.pop(0)
Floatz= float(z)
ax.scatter(Floatx, Floaty, Floatz)
ax.set_xlabel('x label')
ax.set_ylabel('y label')
ax.set_zlabel('z label')
plt.show()
ser.close()
Any advice and suggestion would be appreciated! Thanks! :)