Skip to content

Instantly share code, notes, and snippets.

@erikaris
Last active November 24, 2017 07:57
Show Gist options
  • Save erikaris/adde82c492981ec8ccbc640441205aa8 to your computer and use it in GitHub Desktop.
Save erikaris/adde82c492981ec8ccbc640441205aa8 to your computer and use it in GitHub Desktop.
# Modified from Coursera
# how to do an animation in matplotlib:
# step 1: import the animation module
import matplotlib.animation as animation
import numpy as np
import matplotlib.pyplot as plt
# another way to import:
# from matplotlib animation import FuncAnimation
# step 2: define the number of frames.
n = 100
# step 3: prepare the data to plot
# draw sample from standardized normal distribution
x = np.random.randn(n)
# step 4: prep the figure
fig = plt.figure()
# step 5: prep the callable function that will do the plotting
# the argument must be the next value in frames
def update(curr): #? how the info about the curr can be passed?
# check if animation is at the last frame, and if so, stop the animation a.
if curr == n:
a.event_source.stop()
plt.cla() # each iteration will draw a new axes (frame), thus we have to clear the prev frame first
bins = np.arange(-4, 4, 0.5) # the bins edge range from -4 to 4 with a step of 0.5
plt.hist(x[:curr], bins=bins) # each iteration will plot the 0th - the(curr-1)th elements of the list x.
# thus, data plotted in current iteration = data plotted in prev iteration + 1
plt.axis([-4, 4, 0, 30]) # set the lim for x & y axis
plt.gca().set_title('Sampling the Normal Distribution') # plot title
plt.gca().set_ylabel('Frequency') # y axis title
plt.gca().set_xlabel('Value') # x axis title
plt.annotate('n = {}'.format(curr), [3, 27]) # give annotation to the plot. --> similar to plt.text.
# step 6: create the animation by calling the FuncAnimation
a = animation.FuncAnimation(fig, update, interval=100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment