import math
import matplotlib
from matplotlib.animation import FuncAnimation as FA
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter as FF
import numpy as np
import random
import seaborn as sns
#Initiate starting points
ends = []
#10,000,000 grasshoppers
for g in range(10000000):
land = random.random()
ends.append(land)
#Updates a list of grasshopper ending points with new ending points based on constraints of grasshopper's jump.
def updateEnds(ends):
new_ends = []
for end in ends:
#left side.
if end < 0.2:
new_ends.append(random.uniform(0, end+0.2))
#Right side.
elif end > 0.8:
new_ends.append(random.uniform(end-0.2, 1))
#Anywhere else.
else :
new_ends.append(random.uniform(end-0.2, end+0.2))
return(new_ends)
def grasshopperHops() :
#Plot Setup.
sns.set()
fig = plt.figure(figsize = (14, 8))
#One Artist:
#Bar Plot.
bars = plt.hist(x = [],
#Number of bins.
bins = (np.arange(1001))/1000,
align = "mid",
edgecolor = '#1F968B',
color = '#1F968B')
def animate(i):
global ends
plt.cla()
plt.suptitle("Where Does The Grasshopper End Up?", fontsize = 18)
if i == 0 :
plt.title("Grasshopper Jumps: 0", fontsize = 13)
else :
plt.title("Grasshopper Jumps: %.0f" % (i), fontsize = 13)
#Update jump distribution
ends = updateEnds(ends)
plt.xlabel("Location on Beam (meters)", fontsize = 18)
plt.ylabel("Probability", fontsize = 18)
plt.yticks([], [])
bars = plt.hist(x = ends,
#Number of bins.
bins = (np.arange(1001))/1000,
align = "mid",
edgecolor = '#1F968B',
color = '#1F968B')
return bars
#Run animation, 10 jumps, long pause.
anim = FA(fig = fig,
func = animate,
init_func = None,
frames = 11,
interval = 2000,
blit = False)
#Save animation.
anim.save('2022.06.10 Classic.mp4');
grasshopperHops()