I'm trying to process data within the matplotlib.FuncAnimation function. Anyhow, the data is iterable and I'm struggling to get the animation function in python to iterate over an external variable. In this case the Ball_1 variable.
The error this throws back is the following: 'UnboundLocalError: local variable 'ball_1' referenced before assignment'
I hindsight this isn't the best way to do it. However I would like to know if it is possible?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
ax.set_xlim(0,100)
ax.set_ylim(0,100)
# acc,vel,disp (x,y)
ball_1 = [[0,0,40],[0,0,100]]
ball_1_plot, = ax.plot(ball_1[0][2],ball_1[1][2], "o")
#
def init_DEM():
ball_1 = [[0,0,40],[0,0,100]]
return ball_1_plot,
def DEM(step):
TS = step/10
# Ball_1
# Contact
grav = -9.81
# Acceleration
acc_x = 0
acc_y = grav
# Velocity
vel_x = ball_1[0][1] + acc_x*TS
vel_y = ball_1[1][1] + acc_y*TS
# Position
pos_x = vel_x * TS + 0.5 * acc_x * TS * TS + ball_1[0][2]
pos_y = vel_y * TS + 0.5 * acc_y * TS * TS + ball_1[1][2]
print(pos_x)
# Update
ball_1 = [[acc_x,vel_x,pos_x],[acc_y,vel_y,pos_y]]
# Update animation
ball_1_plot.set_xdata(ball_1[0][2])
ball_1_plot.set_xdata(ball_1[1][2])
return ball_1_plot,
animation_run = FuncAnimation(fig,func=DEM, frames=[10,20], init_func=init_DEM, interval=10)
plt.show()