#python #python-3.x #numpy #matplotlib #pyserial
#python #python-3.x #numpy #matplotlib #pyserial
Вопрос:
Я пытаюсь построить точечный график для датчика вибрации, с которым я работаю на 3D-оси, используя пакет анимации в matplotlib.
Ниже приведен код
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
import serial
seru = serial.Serial('COM6', 115200)
xyz = []
def update_lines(num):
rmsX,rmsY,rmsZ = vib_sense()
xyz = np.array([[rmsX],[rmsY],[rmsZ]]) # replace this line with code to get data from serial line
print(xyz)
text.set_text("{:d}: [{:.0f},{:.0f},{:.0f}]".format(num,rmsX,rmsY,rmsZ)) # for debugging
'''
x.append(rmsX)
y.append(rmsY)
z.append(rmsZ)
'''
graph._offsets3d = (xyz)
return graph,
def vib_sense():
while True:
s = seru.read(54)
if(s[0] == 126):
if(s[15] == 127):
if(s[22]== 8):
rms_x = ((s[24]*65536) (s[25]*256) s[26])/1000
rms_y = ((s[27]*65536) (s[28]*256) s[29])/1000
rms_z = ((s[30]*65536) (s[31]*256) s[32])/1000
return rms_x,rms_y,rms_z
x = [0]
y = [0]
z = [0]
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(111, projection="3d")
graph = ax.scatter(x, y, z, color='orange')
text = fig.text(0, 1, "TEXT", va='top') # for debugging
ax.set_xlim3d(-255, 255)
ax.set_ylim3d(-255, 255)
ax.set_zlim3d(-255, 255)
# Creating the Animation object
ani = animation.FuncAnimation(fig, update_lines, frames=200, interval=50, blit=False)
plt.show()
Результат выглядит следующим образом :
[[ 0.711]
[20.309]
[ 2.369]]
[[ 0.698]
[20.338]
[ 2.275]]
[[ 0.655]
[20.36 ]
[ 2.407]]
[[ 0.751]
[20.328]
[ 2.346]]
[[ 0.757]
[20.312]
[ 2.424]]
[[ 0.705]
[20.345]
[ 2.631]]
[[ 0.679]
[20.306]
[ 2.302]]
А на 3d оси я могу видеть только один параметр за раз
Любые предложения и рекомендации по одновременному отслеживанию всех значений на экране 3D оси будут очень полезны также в какой-то момент график не отвечает и работает очень любые предложения, которые также будут очень полезны
Комментарии:
1. Вы обновляете точечную диаграмму только одной точкой. Если вы хотите увидеть все предыдущие точки, вам нужно будет обновить is этим полным списком предыдущих точек.
Ответ №1:
Наконец, я могу проверить значения, используя приведенный ниже код, который работает нормально, но я не могу повернуть и протестировать.
Код:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
import serial
seru = serial.Serial('COM6', 115200)
x = []
y = []
z = []
fig = plt.figure()
def update_lines(num):
#calling sensor function
rmsX,rmsY,rmsZ = vib_sense()
#Creating Numpy array and appending the values
vib_x= np.array(rmsX)
x.append(vib_x)
vib_y= np.array(rmsY)
y.append(vib_y)
vib_z= np.array(rmsZ)
z.append(vib_z)
print(x)
print(y)
print(z)
ax = fig.add_subplot(111, projection='3d')
ax.clear()
#Limit the Graph
ax.set_xlim3d(0, 100)
ax.set_ylim3d(0, 100)
ax.set_zlim3d(0, 100)
#for line graph
graph = ax.plot3D(x,y,z,color='orange',marker='o')
#For Scatter
# graph = ax.scatter3D(x,y,z,color='orange',marker='o')
return graph
def vib_sense():
while True:
s = seru.read(54)
if(s[0] == 126):
if(s[15] == 127):
if(s[22]== 8):
rms_x = ((s[24]*65536) (s[25]*256) s[26])/1000
rms_y = ((s[27]*65536) (s[28]*256) s[29])/1000
rms_z = ((s[30]*65536) (s[31]*256) s[32])/1000
'''
max_x = ((s[33]*65536) (s[34]*256) s[35])/1000
max_y = ((s[36]*65536) (s[37]*256) s[38])/1000
max_z = ((s[39]*65536) (s[40]*256) s[41])/1000
min_x = ((s[42]*65536) (s[43]*256) s[44])/1000
min_y = ((s[45]*65536) (s[46]*256) s[47])/1000
min_z = ((s[48]*65536) (s[49]*256) s[50])/1000
ctemp = ((s[51]*256) s[52])
battery = ((s[18]*256) s[19])
voltage = 0.00322*battery
'''
return rms_x,rms_y,rms_z
# Creating the Animation object
ani = animation.FuncAnimation(fig, update_lines, frames=200, interval=5, blit=False)
plt.show()
Вывод: