2

I need to animate (at least) two 3D lines. I have list of x,y,z coordinates for each one (the second one marked as xj, yj, zj). I tried to fallow this answer https://stackoverflow.com/a/23065440 as a guide line but can´t make it work. Here´s my code with fake values:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d as ax3d
from matplotlib import animation
x=[1,2,3,4,5]
y=[1,2,3,4,5]
z=[1,2,3,4,5]

xj = [5,4,3,2,1]
yj = [5,4,3,2,1]
zj = [5,4,3,2,1]

N=len(x)
fig = plt.figure()
ax1 = ax3d.Axes3D(fig)
line, = ax1.plot([], [], lw=2, linestyle= "-")

ax1.set_xlim(0,6)
ax1.set_ylim(0,6)
ax1.set_zlim(0,6)
ax1.set_xlabel("x")
ax1.set_ylabel("y")
ax1.set_zlabel("z")
ax1.text2D(0, 0, "Title", transform=ax1.transAxes)

plotlays, plotcols = [2], ["orange","black"]
lines = []
for index in range(2):
    lobj = ax1.plot([],[],lw=2,color=plotcols[index])[0]
    lines.append(lobj)

def init():
    for line in lines:
        line.set_data([],[])
        line.set_3d_properties([])
    return lines

def animate(i):
    A1 = xj[:i]
    B1 = yj[:i]
    C1 = zj[:i]

    A2 = x[:i]
    B2 = y[:i]
    C2 = z[:i]

    xlist = [A1, A2]
    ylist = [B1, B2]
    zlist = [C1, C2]

    for lnum,line in enumerate:
        line.set_data(xlist[lnum], ylist[lnum])
        line.set_3d_properties(zlist[lnum])
    return lines

anim = animation.FuncAnimation(fig, animate,init_func=init, frames=N , interval=5, blit=True)

plt.show()

All I get is blank chart and then it crashes. I can´t figure out where the problem is. Any help would be much appreciated.

BigDLA
  • 21
  • 3
  • I'm not sure if this is the cause of your problem or it's just your typo: you didn't enumerate anything. I saw some animations on "Jupyter notebook" with `for lnum,line in enumerate(lines):`, though I'm not sure if that's the result you want. The animation is there but might be hard to see because your two lines eventually overlap and your interval is relatively small. – Y. Luo Jul 20 '17 at 21:47
  • Ok, now I feel really stupid. :D Thanks a lot it actually solved the problem :D – BigDLA Jul 20 '17 at 21:53

0 Answers0