steps = 120
h = 1.5
p = (0., 0.)
fig, ax = subplots(figsize=(6,6))
ax.set_aspect('equal')
point, = plot([],[],'go', ms=3)
line, = plot([],[],'k')
line2, = plot([],[],'r')
rect = gca().add_patch(Rectangle((0,0), 0,0, fill=False, ec='y'))
stumble, successes, stepsizes = calculate_stumbledown(lake_depth, p, h, steps)
max_steps = ones(2)*stepsizes[0]
maxx, maxy = amax(stumble,axis=0) + max_steps
minx, miny = amin(stumble,axis=0) - max_steps
xlim(minx,maxx)
ylim(miny,maxy)
def init():
point.set_data([],[])
x = linspace(minx,maxx,100)
y = linspace(miny,maxy,100)
X,Y = meshgrid(x,y)
Z = lake_depth(X,Y)
contourf(x,y,Z,cmap='gist_rainbow', levels = arange(100))
return line, line2, point, rect,
def animate(i):
x,y = stumble[i]
path = stumble[:i+1][successes[:i+1]]
px, py = path[-1]
point.set_data(px,py)
line.set_data(path[:,0], path[:,1])
line2.set_data([px,x], [py,y])
stepsize = stepsizes[i]
rect.set_xy((px-stepsize, py-stepsize))
rect.set_height(2*stepsize)
rect.set_width(2*stepsize)
return line, line2, point, rect,
animation.FuncAnimation(fig, animate, init_func = init, frames = steps, interval = 250, blit = False, repeat = True)