Python中的3D动画:移动点和轨迹

时间:2017-08-21 13:56:41

标签: python python-2.7 animation

我正在从Matlab切换到Python,所以我是一个相对初学者。我想得到的是一个在空间中移动的点的3D动画,为了简单起见沿着螺旋线,以及它的轨迹历史。

基于此示例http://matplotlib.org/examples/animation/simple_3danim.html,我提供了以下代码:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as 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 for plotting:
def update_auv(num, dataLines, lines) :
    for 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 for plotting:
def update_trj(num, dataLines, lines) :
    for 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 to the figure
fig = plt.figure()
ax = p3.Axes3D(fig)

# Define no. data points and 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 the 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()

现在,这段代码显示了我想要实现的目标(同时显示移动的身体作为一个点和轨迹的历史),但它有两个主要问题:

  1. 在每个时间步重新计算轨迹,这是低效的,但计算能力应该不是问题;
  2. 更大的问题是点和轨迹之间存在不对齐。我认为它的原因是由于计算简单点位置的计算时间较短。
  3. 另一种选择可能是他们在这里做的事情:Animate a python pyplot by moving a point plotted via scatter,但说实话,我更愿意找到一种方法来创建带有时间戳的动画。这样,我只需要计算一次轨迹,并且可以在每个新的时间步骤更新点的位置。

    感谢您的帮助!

0 个答案:

没有答案
相关问题