Python Forum

Full Version: Create Animated Scatter plot in 3d using matplotlib in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, with the reference link I am trying to plot a scatter graph for the sensor I am working with in 3d axis using animation package in matplotlib.

Mentioned below is the code I am

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)

def update_lines(num):
    rmsX,rmsY,rmsZ = vib_sense()
    dx, dy, dz = np.meshgrid([[rmsX],[rmsY],[rmsZ]])  # replace this line with code to get data from serial line
    print(dx)
    #print(dy)
    #print(dz)
    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 = (x, y, z)
    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
                    rmsValueX = 0;
                    rmsValueY = 0;
                    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()
Error
== RESTART: C:\Users\Misha\Desktop\test\Xbee Test\Matplotlib\phase_test.py ==
Traceback (most recent call last):
  File "C:\Users\Misha\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\cbook\__init__.py", line 215, in process
    func(*args, **kwargs)
  File "C:\Users\Misha\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\animation.py", line 999, in _start
    self._init_draw()
  File "C:\Users\Misha\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\animation.py", line 1740, in _init_draw
    self._draw_frame(next(self.new_frame_seq()))
  File "C:\Users\Misha\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\animation.py", line 1762, in _draw_frame
    self._drawn_artists = self._func(framedata, *self._args)
  File "C:\Users\Misha\Desktop\test\Xbee Test\Matplotlib\phase_test.py", line 11, in update_lines
    dx, dy, dz = np.meshgrid([[rmsX],[rmsY],[rmsZ]])  # replace this line with code to get data from serial line
ValueError: not enough values to unpack (expected 3, got 1)
>>> 
whenever I am trying to make this work my code is not responding properly and I guess the problem I am facing is under Update line function where I am not properly initializing the values

Any advice on this will be really grateful