Skip to content

Instantly share code, notes, and snippets.

@peterhil
Forked from ChrisBeaumont/mandel.py
Created November 17, 2012 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhil/4095130 to your computer and use it in GitHub Desktop.
Save peterhil/4095130 to your computer and use it in GitHub Desktop.
interactive Mandelbrot numba example
"""From the numba examples
(https://github.com/numba/numba/blob/master/examples/mandel.py),
but tweaked to recalculate on the fly as the viewport changes"""
from numba import autojit
import numpy as np
from pylab import imshow, jet, show, ion
import matplotlib.pyplot as plt
@autojit
def mandel(x, y, max_iters):
"""
Given the real and imaginary parts of a complex number,
determine if it is a candidate for membership in the Mandelbrot
set given a fixed number of iterations.
"""
i = 0
c = complex(x,y)
z = 0.0j
for i in range(max_iters):
z = z*z + c
if (z.real*z.real + z.imag*z.imag) >= 4:
return i
return 255
@autojit
def create_fractal(min_x, max_x, min_y, max_y, image, iters):
height = image.shape[0]
width = image.shape[1]
pixel_size_x = (max_x - min_x) / width
pixel_size_y = (max_y - min_y) / height
for x in range(width):
real = min_x + x * pixel_size_x
for y in range(height):
imag = min_y + y * pixel_size_y
color = mandel(real, imag, iters)
image[y, x] = color
return image
def update_axes(ax):
xlim = ax.get_xlim()
ylim = ax.get_ylim()
trans = ax.transAxes
ext = trans.transform([[1, 1]]) - trans.transform([[0, 0]])
nx = ext[0, 0]
ny = ext[0, 1]
image = np.zeros((ny, nx), dtype=np.uint8)
create_fractal(xlim[0], xlim[1], ylim[0], ylim[1], image, 255)
ax.clear()
ax.imshow(image, extent=[xlim[0], xlim[1], ylim[0], ylim[1]], zorder=0)
ax.figure.canvas.draw()
image = np.zeros((500, 750), dtype=np.uint8)
artist = plt.imshow(create_fractal(-2.0, 1.0, -1.0, 1.0, image, 255),
extent=[-2.0, 1.0, -1.0, 1.0])
ax = plt.gca()
ax.figure.canvas.mpl_connect('button_release_event', lambda x: update_axes(ax))
ax.set_title('Zoom in')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment