Python Forum
Error when animating 3D plot
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error when animating 3D plot
#1
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()
Reply
#2
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
Tee likes this post
Reply
#3
(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.
Reply
#4
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
Reply
#5
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.    

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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Likert survey data plot error Andrzej_Andrzej 6 1,451 Jul-16-2023, 10:11 PM
Last Post: deanhystad
  How to plot intraday data of several days in one plot mistermister 3 2,933 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  How to plot vertically stacked plot with same x-axis and SriMekala 0 1,944 Jun-12-2019, 03:31 PM
Last Post: SriMekala
  scatter plot error - possibly data type - TypeError: nan is not a string chudson 1 6,549 Mar-24-2019, 11:48 AM
Last Post: chudson
  Animating Random Walk Excelsiscelia 2 5,235 Nov-24-2018, 08:53 AM
Last Post: Excelsiscelia

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020