Skip to content

Instantly share code, notes, and snippets.

@ceptreee
Last active August 12, 2018 19:09
Show Gist options
  • Save ceptreee/375730d716682c3dd6ecb943183a9e7a to your computer and use it in GitHub Desktop.
Save ceptreee/375730d716682c3dd6ecb943183a9e7a to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from numba import jit
# Julia set
# https://en.wikipedia.org/wiki/Julia_set#Quadratic_polynomials
@jit
def JuliaSet(N,Nx,Ny,z,c):
M = Nx*Ny
Z = z*np.ones(M,dtype=complex)
A = np.zeros(M)
for i in range(M):
for n in range(N):
if np.abs(Z[i]) < 2:
Z[i] = Z[i]**2 + c
else:
A[i] = n
break
A = A.reshape(Ny,Nx)
return A
N = 200
h = 0.001
xlm = [-1.6,1.6]
ylm = [-1.2,1.2]
c = -0.763 + 0j
x = np.arange(xlm[0],xlm[1]+h,h)
y = np.arange(ylm[0],ylm[1]+h,h)
Nx = len(x)
Ny = len(y)
xx, yy = np.meshgrid(x,y)
z = (xx + yy*1j).flatten()
A = JuliaSet(N,Nx,Ny,z,c)
C = c - 0.0025*np.arange(0,200)*1j
cmap = 'hot'
interpolation = 'spline16'
plt.close('all')
fig = plt.figure()
im = plt.imshow(A,extent=[xlm[0],xlm[1],ylm[0],ylm[1]],cmap=cmap,interpolation=interpolation)
plt.clim([0,100])
plt.axis('off')
interval = 120
def update(i):
c = C[i]
A = JuliaSet(N,Nx,Ny,z,c)
im.set_data(A)
ani = animation.FuncAnimation(fig, update, frames=len(C),interval=interval)
ani.save('c=%+.3f%+.3fi' % (c.real,c.imag)+'.mp4', writer="ffmpeg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment