import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
fig = plt.figure(figsize=(4, 4), dpi=80)
ax = plt.gca()
scatter = ax.scatter([], [], s=0.1)
x = dz * np.random.random(N)
y = dz * np.random.random(N)
z = Lz * np.random.random(N)
vx = np.random.normal(0, Tw, N)
vy = np.random.normal(0, Tw, N)
vz = np.random.normal(0, Tw, N)
def update(frame):
""" Update function for animation """
global x, y, z, scatter
# Evolve
for i in range(Nt):
# Drift
x += dt * vx
y += dt * vy
z += dt * vz
# Collide specular wall (z=Lz)
# Trace the straight-line trajectory to the top wall, bounce it back
hit_top = z > Lz
dt_ac = (z[hit_top] - Lz) / vz[hit_top] # time after collision
vz[hit_top] = -vz[hit_top] # reverse normal component of velocity
z[hit_top] = Lz + dt_ac * vz[hit_top]
# Update scatter plot
scatter._offsets3d = (x, y, z)
return scatter,
# Animation update function
def animate(frame):
update(frame)
return scatter,
# Create the animation
num_frames = Nt
animation = FuncAnimation(fig, animate, frames=num_frames, interval=200)
# Display the animation
plt.show()
I am trying to make animation of particles in a box and I have tried everything that I knew but I can't get it running. I just get this plot.
Please ignore the constants such as N, Tw, etc. I have not added them for brevity