Skip to content

Instantly share code, notes, and snippets.

@IlievskiV
Last active April 19, 2020 06:41
Show Gist options
  • Save IlievskiV/931a766d1a2d83a8971841c11e1f5fbf to your computer and use it in GitHub Desktop.
Save IlievskiV/931a766d1a2d83a8971841c11e1f5fbf to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig = plt.figure(figsize=(21, 10))
ax = plt.axes(xlim=(0, 1))
line, = ax.step([], [], where='mid', color='#0492C2')
# formatting options
ax.set_xticks(np.linspace(0,1,11))
ax.set_xlabel('Time', fontsize=30)
ax.set_ylabel('Value', fontsize=30)
ax.tick_params(labelsize=22)
ax.grid(True, which='major', linestyle='--', color='black', alpha=0.6)
# initialization function
def init():
line.set_data([], [])
return line,
# animation function
def animate(i):
np.random.seed(42)
y, epsilon = brownian_motion((i + 1) * 10, 1 ,1)
tr = np.linspace(0.0, 1, (i + 1) * 10 + 1)
ax.set_title('Brownian Motion for {} steps'.format((i + 1) * 10), fontsize=40)
ax.set_ylim((np.min(y) - 0.1, np.max(y) + 0.1))
line.set_data(list(tr), list(y))
return line,
# call the animator
anim = FuncAnimation(fig, animate, init_func=init, frames=200, interval=150, blit=True)
anim.save('brownian_motion.gif',writer='imagemagick')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment