Python Forum
Create Animated Scatter plot in 3d using matplotlib in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create Animated Scatter plot in 3d using matplotlib in python
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create simple live plot of stock data dram 2 2,907 Jan-27-2023, 04:34 AM
Last Post: CucumberNox
  How do you create a scatterplot of dataframe using matplotlib? asdad 2 856 Dec-07-2022, 04:53 PM
Last Post: Larz60+
Shocked plt.scatter() errors asja2010 0 1,469 Oct-13-2022, 08:15 AM
Last Post: asja2010
  Graphic line plot with matplotlib, text file in pytho khadija 2 1,375 Aug-15-2022, 12:00 PM
Last Post: khadija
  plot on the same figure using a for loo with matplotlib drSlump 2 1,700 Oct-13-2021, 07:11 AM
Last Post: drSlump
  matplotlib, no plot when using Figure ajvperth 2 1,673 Oct-12-2021, 02:29 PM
Last Post: ajvperth
  How to invert scatter plot axis Mark17 3 2,494 Sep-22-2021, 04:45 PM
Last Post: jefsummers
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,214 Mar-11-2021, 10:52 AM
Last Post: buran
  Python Matplotlib: How do I save (in pdf) all the graphs I create in a loop? JaneTan 1 8,849 Feb-28-2021, 06:20 PM
Last Post: Larz60+
  Python Matplotlib: How do I plot lines with different end point in the same graph? JaneTan 0 1,575 Feb-28-2021, 11:56 AM
Last Post: JaneTan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020