我正在尝试使用matplotlib中的动画包为在3d轴内工作的振动传感器绘制散点图。
下面提到的是代码
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轴屏幕上同时监视所有值的任何建议和建议也将非常有帮助,在某些情况下,情节没有响应,并且对任何建议都将起作用也很有帮助
答案 0 :(得分:0)
最后,我可以使用下面提到的代码来测试值,该代码可以正常工作,但是我不能旋转和测试。
代码:
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()
输出: