I am trying to set up a 3D scatter plot animation, following the example here: Matplotlib 3D scatter animations
I put together the following code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
def terminal_w(r):
"""calculation of droplet terminal fallspeed"""
w=X1*r**2
return(w)
class drop():
"""class of a drop"""
def __init__(self,xpos,ypos,zpos,rad):
self.x,self.y,self.z=xpos,ypos,zpos # position
self.r=rad # radius
self.w=terminal_w(rad) # velocity
def main():
# dimensions of the domain
global xsize,ysize,zsize
global X1,X2,X3
global dt # timestep
global rho_l
xsize,ysize,zsize=1.,1.,100. # domain size in meters
X1,X2,X3=1.2e8,8e3,250. # terminal velocity coeffs
dt=10.0
rho_l=1000. # density of liquid water
#
# Student exercise 1: change radii and liquid water amount:
#
liq=1.e-3 # cloud liq water content in kg/m**3
rsmall=5.e-6 # microns
rlarge=20.e-6
# L=N*rho_l*4/3 pi r^3
ndrop=10 # total number of drops
# initial number of large drops with rlarge
# not used if distrbution of drop sizes assumed
nlarge=1
# initial random positions:
# could use a dictionary, but don't want to lose numpy advantage?
dropx=np.random.uniform(low=0,high=xsize,size=ndrop)
dropy=np.random.uniform(low=0,high=ysize,size=ndrop)
dropz=np.random.uniform(low=0,high=zsize,size=ndrop)
# one large drop falling through a cloud of small drops
dropr=np.full(ndrop,fill_value=rsmall)
dropr[-nlarge:]=rlarge
# Student exercise 2: change distribution:
# can insert code here to simply set a distribution of radii:
# set arrange from lognormal distribution,
# dropr=np.random.DIST(moments)
# initial drop conditions
drops=drop(dropx,dropy,dropz,dropr)
# set up plot window
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(0,xsize)
ax.set_ylim(0,ysize)
ax.set_zlim(0,zsize)
sc3d=ax.scatter(drops.x,drops.y,drops.z,c='blue',marker='o')
title = ax.set_title('3D Test')
def animate(i):
timestep(drops)
print(drops.z[0])
sc3d._offsets3d=(drops.x,drops.y,drops.z) # update the data
title.set_text('3D Test, time={}'.format(i))
ani = animation.FuncAnimation(fig, animate,
interval=25,
blit=False)
plt.show()
def timestep(drops):
#print(drops.w)
drops.z-=dt*drops.w
#print(drops.z)
main()
which produces a 3D scatter plot, but only animates ONE of the markers, even though all entries are having their z coordinates updated (when I uncheck the print statements).
Even more bizarrely, if I comment out the statement
title.set_text('3D Test, time={}'.format(i))
in the animate function, none of the drops are animated. I don't understand why adding the title statement allows one marker to animate, and also I can't see how my code differs from the example which functions for me and animates all points...