Real time 3D scatter plot with matplotlib

Hey guys, has anyone figured out how to display a realtime 3D scatter plot using matplotlib. This is what I have at the moment, which doesn’t work. Just displays an empty 3d plot, and it doesn’t update. video is just a sequence of frames, each containing 3D coordinates and a hand skeleton. I am trying to eventually visualize a hand gesture in realtime. Any recommendations on how to make it work with streamlit. Otherwise, any recommendations on other frameworks other than matplotlib that can handle realtime plots ? Thanks

x = []
y = []
z = []


fig = plt.figure(figsize=(10, 10))
ax = plt.axes(projection = '3d')
graph = ax.scatter([], [], [], s=50, color='orange')  # s argument here 

c2.pyplot(fig)
for i in range(len(video)):
    print(i)
    frame=video[i]
    for num in range(0,len(frame)-1) :
        #print(i)
        dx,dy,dz=frame[num]
        dx_n,dy_n,dz_n=frame[num+1]
        # Visualize 3D scatter plot
        x.append(dx) 
        y.append(dy) 
        z.append(dz)
        if i+1==len(frame)-1:       
            x.append(dx_n) 
            y.append(dy_n) 
            z.append(dz_n)
        # Give labels
        ax.set_xlabel('x')
        ax.set_ylabel('y')
        ax.set_ylabel('z') 
    graph._offsets3d = (x, y, z)

    time.sleep(0.01)

If by “real-time” you just mean that it updates frame-by-frame as it goes through the video, you can create a placedholder st.empty object, and then re-plot the image over and over with that placeholder. For example,

import time

import numpy as np
import streamlit as st
from matplotlib import pyplot as plt

video = np.random.randn(100, 10, 3)

x = []
y = []
z = []


fig = plt.figure(figsize=(10, 10))
ax = plt.axes(projection="3d")
graph = ax.scatter([], [], [], s=50, color="orange")  # s argument here

placeholder = st.empty()

for i in range(len(video)):
    frame = video[i]
    for num in range(0, len(frame) - 1):
        dx, dy, dz = frame[num]
        dx_n, dy_n, dz_n = frame[num + 1]
        # Visualize 3D scatter plot
        x.append(dx)
        y.append(dy)
        z.append(dz)
        if i + 1 == len(frame) - 1:
            x.append(dx_n)
            y.append(dy_n)
            z.append(dz_n)
        # Give labels
        ax.set_xlabel("x")
        ax.set_ylabel("y")
        ax.set_ylabel("z")
    graph._offsets3d = (x, y, z)

    placeholder.pyplot(fig)

    time.sleep(0.01)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.