3D Animation in Python: Moving Point and Trajectory -
i switching matlab python, relative beginner. 3d animation of point moving in space, along helix simplicity, , history of trajectory.
based on example http://matplotlib.org/examples/animation/simple_3danim.html, have come following code:
import numpy np import matplotlib.pyplot plt import mpl_toolkits.mplot3d.axes3d p3 import matplotlib.animation animation ############################################################################### # create helix: 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 # update auv position plotting: def update_auv(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 # update trajectory plotting: def update_trj(num, datalines, lines) : line, data in zip(lines, datalines) : line.set_data(data[0:2, :num]) line.set_3d_properties(data[2,:num]) return lines ############################################################################### # attach 3d axis figure fig = plt.figure() ax = p3.axes3d(fig) # define no. data points , create helix: n = 100 data = [make_helix(n)] # create line objects: auv = [ax.plot(data[0][0,0:1], data[0][1,0:1], data[0][2,0:1], 'ro')[0]] trj = [ax.plot(data[0][0,0:1], data[0][1,0:1], data[0][2,0:1])[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_auv = animation.funcanimation(fig, update_auv, n, fargs=(data, auv), interval=50, blit=false) #repeat=false, ani_trj = animation.funcanimation(fig, update_trj, n, fargs=(data, trj), interval=50, blit=false) #repeat=false, plt.show()
now, code shows trying achieve (show moving body point , history of trajectory @ same time), has 2 major problems:
- the trajectory recalculated @ every time step, inefficient, computing power should not problem;
- the bigger problem there misalignment between point , trajectory. think reason due lower computational time associated calculation of position of simple point.
an alternative here: animate python pyplot moving point plotted via scatter, honest, prefer find way create animation time stamps. way, have calculate trajectory once , can update position of point @ every new time step.
thank help!
wiki
Comments
Post a Comment