Sounds like you need a 3D animation. Hopefully this is what you want:
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))
Edit: to show line growing rather than line moving.
The basic idea is to add a point to the line for each frame loop rather than changing the start and end points of the line:
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))
Edit 2: Update axis limit; draw lines with different color; skip even lines.
The basic idea is to:
- Pass
ax
as one of fargs
so that you can update axis limit with ax
.
- Set up your lines as 4 empty lines and show (or skip) corresponding lines in each frame. By default, different lines will have different color.
Here is some codes to start with. Just to be clear, the following code is not the best in design and may or may not fit your needs. But I think it's a good starting point.
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)