Python Matplotlib FuncAnimation:在两个移动点之间绘制一条线

时间:2019-03-07 08:04:41

标签: python matplotlib

我要解决的问题如下:

我在空间中任意移动了两个点,我需要绘制一条线将一个点链接到另一个点。

该代码似乎可以正常工作,但是编写得不好。有人可以帮助我吗?

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

###############################################################################

s="\n"
print(s*100)

###############################################################################

# Create Trajectory H:
def make_trajectory_H(n):
    x_0_H = -0.1
    y_0_H = 0
    z_0_H = 0
    x_H = x_0_H*np.sin(2*np.pi*f*t)
    y_H = y_0_H*np.sin(2*np.pi*f*t)
    z_H = z_0_H*np.sin(2*np.pi*f*t)
    trajectory_H = np.vstack((x_H, y_H, z_H))

    return trajectory_H

# Create Trajectory B:
def make_trajectory_B(n):
    x_0_B = -0.1
    y_0_B = 0
    z_0_B = 0
    x_B = x_0_B*np.sin(2*np.pi*f*t+1)
    y_B = y_0_B*np.sin(2*np.pi*f*t)
    z_B = z_0_B*np.sin(2*np.pi*f*t)-.3
    trajectory_B = np.vstack((x_B, y_B, z_B))

    return trajectory_B

# Update AUV position for plotting:
def update(num, data_H, data_B, auv_H, auv_B, trj_H, trj_B) :

    for line, data in zip(auv_H, data_H) :
        line.set_data(data[0:2, num-1:num])
        line.set_3d_properties(data[2,num-1:num])

    for line, data in zip(auv_B, data_B) :
        line.set_data(data[0:2, num-1:num])
        line.set_3d_properties(data[2,num-1:num])

    for line, data in zip(trj_H, data_H) :
        line.set_data(data[0:2, :num])
        line.set_3d_properties(data[2,:num])

    for line, data in zip(trj_B, data_B) :
        line.set_data(data[0:2, :num])
        line.set_3d_properties(data[2,:num])

    auv_L=ax.plot([data_L[0][0][num-1], data_L[1][0][num-1]], [data_L[0][1][num-1], data_L[1][1][num-1]], [data_L[0][2][num-1], data_L[1][2][num-1]],'-k')
    ax.lines.pop(3)

    return auv_H, auv_B, trj_H, trj_B,

###############################################################################

# Attach 3D axis to the figure

fig = plt.figure()
ax = p3.Axes3D(fig)

# Define no. temporal steps, max time of observation and create trajectory:

n = 100
t_max = 20
t = np.linspace(0, t_max, n)

f = 50

data_H = [make_trajectory_H(n)]
data_B = [make_trajectory_B(n)]
data_L = data_H+data_B

# data_L[0][-][...] -> H data
# data_L[1][-][...] -> B data
# data_L[-][0][x_1, x_2, ..., x_n] -> x data
# data_L[-][1][y_1, y_2, ..., y_n] -> y data
# data_L[-][2][z_1, z_2, ..., z_n] -> z data

# Create first line objects:

auv_H = [ax.plot(data_L[0][0], data_L[0][1], data_L[0][2], 'ro')[0]]
auv_B = [ax.plot(data_L[1][0], data_L[1][1], data_L[1][2], 'bo')[0]]
#auv_L = [ax.plot([data_L[0][0], data_L[1][0]], [data_L[0][1], data_L[1][1]], [data_L[0][2], data_L[1][2]],'-k')[0]]

trj_H = [ax.plot(data_L[0][0], data_L[0][1], data_L[0][2],'r',alpha=1)[0]]
trj_B = [ax.plot(data_L[1][0], data_L[1][1], data_L[1][2],'b',alpha=1)[0]]

# Set the axes properties

ax.set_xlim3d([-.15, 0.15])
ax.set_xlabel('X')

ax.set_ylim3d([-.15, 0.15])
ax.set_ylabel('Y')

ax.set_zlim3d([-.15, 0.15])
ax.set_zlabel('Z')

ax.set_title('3D Test')

# Creating the Animation object

ani = animation.FuncAnimation(fig, update, n, fargs=(data_H, data_B, auv_H, auv_B, trj_H, trj_B),
                              interval=500, blit=False, repeat=False)

plt.show()

它做了它应该做的事,但是我是通过“反复试验”做到的​​。有人可以告诉我编写此例程的“正确方法”吗?

0 个答案:

没有答案