I am trying to animate a random walk in 2 dimensions. I've been able to do this using turtle, but I want to learn this animation package. When I run the following code, I get a blank plane and I'm not sure why. Any suggestion is greatly appreciated!
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib.animation import FuncAnimation
import random
x = 0
y = 0
x_data=[]
y_data=[]
fig, ax = plt.subplots()
ax.set_xlim(-10,10)
ax.set_ylim(-10,10)
line, = ax.plot(0,0)
def init():
line.set_data([],[])
return line,
def move_random(s,t):
direction = random.randint(1,4)
if direction == 1:
s += 1
elif direction == 2:
t += 1
elif direction == 3:
s+= -1
elif direction == 4:
t+= -1
def animate_rw(i):
move_random(x,y)
x_data.append(x)
y_data.append(y)
line.set_xdata(x_data)
line.set_ydata(y_data)
return line,
anim = animation.FuncAnimation(fig, animate_rw, init_func = init, frames = 200, interval = 10)
plt.show()