Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Plot Weierstrass' monster
def Weierstrass(x, reps=10):
res = np.zeros(x.shape[0])
for i in range(reps):
num = x*(3**i)*np.pi
denom = 2.0**i
res = res + np.cos(num)/denom
return res
title = '$f(x) = {cos(3x\pi)}/{2} + {cos(3^2x\pi)}/{2^2} + {cos(3^3x\pi)}/{2^3} ...$'
delta = 0.5
bg_color = 'black'
fg_color = 'white'
plt.style.use('dark_background')
for i,lim in enumerate(np.logspace(1,-10,200)):
fig = plt.figure(facecolor=bg_color, edgecolor=fg_color)
axes = plt.axes((0.1, 0.1, 0.8, 0.8), axisbg=bg_color)
axes.xaxis.set_tick_params(color=bg_color, labelcolor=bg_color)
axes.yaxis.set_tick_params(color=bg_color, labelcolor=bg_color)
x = np.linspace(-lim+delta,lim+delta,5000)
axes.plot(x, Weierstrass(x,41),color='red')
axes.set_xlim(-lim+delta,lim+delta)
axes.get_xaxis().get_major_formatter().set_useOffset(False)
axes.get_yaxis().get_major_formatter().set_useOffset(False)
axes.set_title(title)
plt.savefig('Weierstrass_{0:03d}'.format(i))
plt.close()
convert -delay 10 -loop 0 Weierstrass_0* Weierstrass.gif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment