简体   繁体   中英

Create Animated Scatter plot for Vibration Sensor Readings on 3D axis using Python Matplotlib

I am trying to plot a scatter graph for the vibration sensor I am working within the 3d axis using an animation package in matplotlib.

Mentioned below is the code

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()

The result is coming as below :

[[ 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]]

And In 3d axis, I able to see only one parameter at a time

在此处输入图片说明

Any suggestion and advice to monitor all the values at the same time on 3D axis screen will be very helpful also at some time the plot is not responding and working very any suggestion that will also very helpful

Finally I am able to test the values by using mentioned below code which works fine but I am not able to rotate and test.

Code:

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()

Output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM