Python Forum
Error when animating 3D plot - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Error when animating 3D plot (/thread-40272.html)



Error when animating 3D plot - Tee - Jul-03-2023

I am trying to animate a 3D plot. I am getting an error and I can't figure out why.
The program runs without any issues when the dot1 plot is not included. But I get the following error when I include dot1.

Error:
File "/Users/Tee/anaconda3/lib/python3.10/site-packages/mpl_toolkits/mplot3d/art3d.py", line 175, in set_3d_properties zs = np.broadcast_to(zs, len(xs)) TypeError: object of type 'numpy.float64' has no len()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

x = np.sort(10 + np.random.rand(1,100)*5)
y = np.sort(10 + np.random.rand(1,100)*2)
z  = np.sort(20 + np.random.rand(1,100)*10)


def animate(num,x,y, z, line1,dot1):
    
    line1.set_data(np.array([x[0][:num], y[0][:num]]))
    line1.set_3d_properties(z[0][:num])
    
    dot1.set_data(np.array([x[0][num], y[0][num]]))
    dot1.set_3d_properties(z[0][num])
    
    return line1, dot1




fig = plt.figure(7)
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(10, 15)
ax.set_ylim(10, 12)
ax.set_zlim(20, 30)

line1, = ax.plot([],[],[], c='b')
dot1, = ax.plot(x[0][0], y[0][0], z[0][0], linestyle="", marker="o", c='r', linewidth=1)

N = x.size

anim = animation.FuncAnimation(fig, animate, N, fargs=(x,y, z, line1,dot1),interval = 10, blit=False)



# Show the plot!
plt.show()



RE: Error when animating 3D plot - deanhystad - Jul-03-2023

set_3d_properties must expect an array. Supply one. May as well pass arrays to dot1.set_data too.
def animate(num,x,y, z, line1,dot1):
     
    line1.set_data(np.array([x[0][:num], y[0][:num]]))
    line1.set_3d_properties(z[0][:num])
     
    dot1.set_data(np.array([x[0][num:num+1], y[0][num:num+1]]))
    dot1.set_3d_properties(z[0][num:num+1])
     
    return line1, dot1



RE: Error when animating 3D plot - Tee - Jul-03-2023

(Jul-03-2023, 12:02 PM)deanhystad Wrote: set_3d_properties must expect an array. Supply one. May as well pass arrays to dot1.set_data too.
def animate(num,x,y, z, line1,dot1):
     
    line1.set_data(np.array([x[0][:num], y[0][:num]]))
    line1.set_3d_properties(z[0][:num])
     
    dot1.set_data(np.array([x[0][num:num+1], y[0][num:num+1]]))
    dot1.set_3d_properties(z[0][num:num+1])
     
    return line1, dot1

Thank you.


RE: Error when animating 3D plot - deanhystad - Jul-03-2023

Could also do this:
def animate(num,x,y, z, line1,dot1):
     
    line1.set_data(np.array([x[0][:num], y[0][:num]]))
    line1.set_3d_properties(z[0][:num])
     
    dot1.set_data(np.array([[x[0][num]], [y[0][num]]]))
    dot1.set_3d_properties([z[0][num]])
     
    return line1, dot1



RE: Error when animating 3D plot - Tee - Jul-03-2023

I will like to add a vertical line to the plot and I updated my code as shown below:
Please see attached a picture of the desired graph. [attachment=2444]

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

x = np.sort(10 + np.random.rand(1,100)*5)
y = np.sort(10 + np.random.rand(1,100)*2)
z  = np.sort(20 + np.random.rand(1,100)*10)


def animate(num,x,y, z, line1,dot1, vertLine):
    
    line1.set_data(np.array([x[0][:num], y[0][:num]]))
    line1.set_3d_properties(z[0][:num])
    
    dot1.set_data(np.array([x[0][num:num+1], y[0][num:num +1]]))
    dot1.set_3d_properties(z[0][num: num + 1])
    
    # update the data for the vertical lines
    
    vertLine.set_data(np.array([x[0][num:num+1],  x[0][num:num+1]]), np.array([y[0][num:num+1],  y[0][num:num+1]]) )
    #vertLine.set_3d_properties(np.array([np.min(z[0]), z[0][num+1]]))
    vertLine.set_3d_properties([np.min(z[0]), z[0][num + 1]])
    
    return line1, dot1, vertLine




fig = plt.figure(7)
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(10, 15)
ax.set_ylim(10, 12)
ax.set_zlim(20, 30)

line1, = ax.plot([],[],[], c='b')
dot1, = ax.plot(x[0][0], y[0][0], z[0][0], linestyle="", marker="o", c='r', linewidth=1)
vertLine, = ax.plot([x[0][0],x[0][0]],[y[0][0],y[0][0]],[np.min(z[0]),z[0][0]], c='r', alpha=1)

N = x.size

anim = animation.FuncAnimation(fig, animate, N, fargs=(x,y, z, line1,dot1, vertLine),interval = 10, blit=False)
I get the following error -
Error:
/Users/Tee/anaconda3/lib/python3.10/site-packages/mpl_toolkits/mplot3d/proj3d.py:140: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. return np.array([xs, ys, zs, np.ones_like(xs)])
.

I am not sure how to resolve this errror.