Mass17
Mass17

Reputation: 1605

3D plot animation in Python

I have an issue when I try to animate a 3D plot. To give an idea, lets say I have collected 3 data-points from each video frame. Lets say my video contains only 5 frames.

I have extracted the coordinates as follow for example;

x = [[1.0,1.2,1.3], [1.2,1.1,1.3], [1.3,1.2,1.0], [1.2,1.1,1.4], [1.2,1.4,1.1]]
y = [[2.0,2.2,2.3], [2.2,2.1,2.3], [2.3,2.2,2.0], [2.2,2.1,2.4], [2.2,2.4,2.1]]
z = [[3.0,3.2,3.3], [3.2,3.1,3.3], [3.3,3.2,3.0], [3.2,3.1,3.4], [2.2,2.4,2.1]]

len(x) = len(y) = len(z) = 5 (# of frames)

that means in the 1st frame, my coordinates are (1.0, 2.0, 3.0), (1.2, 2.2, 3.2) & (1.3,2.3, 3.3), in the 2nd frame (1.2,2.2,3.2), (1.1,2.1,3.1) & (1.3,2.3, 3.3) and so forth. I tried as follow in the Jupyter notebook; but no success.

import numpy as np                          
import matplotlib.pyplot as plt            
from mpl_toolkits.mplot3d import Axes3D
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation


fig = plt.figure()
ax = p3.Axes3D(fig)
timesteps = 100
border = 2
ax.set_xlim3d([-border, border])
ax.set_ylim3d([-border, border])
ax.set_zlim3d([-border, border])


def animate(i):

    ax.set_xlim3d([-border, border])
    ax.set_ylim3d([-border, border])
    ax.set_zlim3d([-border, border])

    ax.scatter(x[i],y[i],z[i])

anim = animation.FuncAnimation(fig, animate, frames=timesteps, interval=1, blit=False, repeat=False)
anim

Upvotes: 1

Views: 1169

Answers (1)

Mass17
Mass17

Reputation: 1605

I was able to solve it using the following link: see here. I post it here if anyone needs this in the future.

Upvotes: 1

Related Questions