听起来您需要3D animation。希望这是您想要的:
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
def move_curve(i, line, x, y, z):
line.set_data([[x[i], x[i+1]], [y[i],y[i+1]]])
line.set_3d_properties([z[i],z[i+1]])
fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.arange(1,6)
y = np.arange(5,11)
z = np.arange(3,9)
i = 0
line = ax.plot([x[i], x[i+1]], [y[i],y[i+1]], [z[i],z[i+1]])[0]
ax.set_xlim3d([1, 5])
ax.set_ylim3d([5, 10])
ax.set_zlim3d([3, 8])
line_ani = animation.FuncAnimation(fig, move_curve, 4, fargs=(line, x, y, z))
编辑:显示线条增长而不是线条移动。
基本思想是在每一帧循环的线上添加一个点,而不是改变线的起点和终点:
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
def move_curve(i, line, x, y, z):
# Add points rather than changing start and end points.
line.set_data(x[:i+1], y[:i+1])
line.set_3d_properties(z[:i+1])
fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.arange(1,6)
y = np.arange(5,11)
z = np.arange(3,9)
i = 0
line = ax.plot([x[i], x[i+1]], [y[i],y[i+1]], [z[i],z[i+1]])[0]
ax.set_xlim3d([1, 5])
ax.set_ylim3d([5, 10])
ax.set_zlim3d([3, 8])
line_ani = animation.FuncAnimation(fig, move_curve, 5, fargs=(line, x, y, z))
编辑2:更新轴限制;用不同的颜色画线;跳过偶数行。
基本思路是:
- 将
ax 作为fargs 之一传递,以便您可以使用ax 更新轴限制。
- 将您的行设置为 4 个空行,并在每一帧中显示(或跳过)相应的行。默认情况下,不同的线条会有不同的颜色。
这是一些开始的代码。为了清楚起见,以下代码在设计上并不是最好的,可能符合您的需求,也可能不符合您的需求。但我认为这是一个很好的起点。
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
def move_curve(num, ax, lines, x, y, z):
for i in range(num+1):
if i % 2 == 1:
continue
lines[i].set_data([[x[i], x[i+1]], [y[i],y[i+1]]])
lines[i].set_3d_properties([z[i],z[i+1]])
ax.set_xlim3d([1, x[i+1]])
ax.set_ylim3d([5, y[i+1]])
ax.set_zlim3d([3, z[i+1]])
return lines
fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.arange(1,6)
y = np.arange(5,11)
z = np.arange(3,9)
lines = [ax.plot([], [], [])[0] for i in range(4)]
line_ani = animation.FuncAnimation(fig, move_curve, 4, fargs=(ax, lines, x, y, z), repeat=False)