python - 使用 Python Matplotlib 在 3D 轴上为振动传感器读数创建动画散点图
问题描述
我正在尝试使用 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 轴屏幕上同时监视所有值的任何建议和建议都会非常有帮助,有时情节也没有响应并且非常有用任何建议也将非常有帮助
解决方案
最后,我可以使用下面提到的代码来测试这些值,它工作正常,但我无法旋转和测试。
代码:
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()
输出:
推荐阅读
- java - 当 Hibernate 查询缓存处于活动状态时,Wildfly 无法启动
- excel - 从一张纸到另一张的公式
- javascript - 如何从不同设备连接到 Firestore 上的同一文档?
- python - 如何使用numpy分别计算每年的平均每月温度?
- sql - SQL 尽可能快地用依赖于列类型的默认值替换 NULL
- html - 100% 宽度的固定位置元素超出父容器
- javascript - Laravel - DataTables 不会显示结果:“Ajax 错误”和“500 内部服务器错误”
- sql-server - 如何在 SEDE 上对徽章表进行分页?
- angular - 更新后的值未正确显示到网格中
- django - “如何在 django 中获取已经存在的用户购物车”