animation - Animating the motion of a particle in 3D in Python -


i have 3xn array positions vary in time particle. want take coordinates @ each time point, plot position point on 3d axis , repeat, creating animation.

i can create single image of effect using matplotlib's axes3d

x_a = particle_a[:,0] y_a = particle_a[:,1] z_a = particle_a[:,2]  fig = plt.figure() ax = fig.add_subplot(111, projection='3d')   ax.scatter(x_a, y_a, z_a, c='b') plt.show 

'particle_a' array of shape (n,3) n number of timepoints.

how can animate this?

adapting this example matplotlib site can represent particle moving in 3d with:

import numpy np import matplotlib.pyplot plt import mpl_toolkits.mplot3d.axes3d p3 import matplotlib.animation animation  def make_helix(n):     theta_max = 8 * np.pi     theta = np.linspace(0, theta_max, n)     x, y, z = theta, np.sin(theta), np.cos(theta)     helix = np.vstack((x, y, z))      return helix  def update_lines(num, datalines, lines) :     line, data in zip(lines, datalines) :         line.set_data(data[0:2, num-1:num])         line.set_3d_properties(data[2,num-1:num])     return lines  # attach 3d axis figure fig = plt.figure() ax = p3.axes3d(fig)  n = 100 data = [make_helix(n)]  lines = [ax.plot(data[0][0,0:1], data[0][1,0:1], data[0][2,0:1], 'o')[0]]  # setthe axes properties ax.set_xlim3d([0.0, 8*np.pi]) ax.set_xlabel('x')  ax.set_ylim3d([-1.0, 1.0]) ax.set_ylabel('y')  ax.set_zlim3d([-1.0, 1.0]) ax.set_zlabel('z')  ax.set_title('3d test')  # creating animation object ani = animation.funcanimation(fig, update_lines, n, fargs=(data, lines),                               interval=50, blit=false) plt.show() 

(replace helix own data , set axes limits accordingly).


Comments