Images and contours
Let us take a look at some examples of visualizing arrays as images. The following function will create a matrix of color values for the Mandelbrot fractal. Here we consider a fixed point iteration, that depends on a complex parameter c:
Depending on the choice of this parameter it may or may not create a bounded sequence of complex values zn .
For every value of c, we check if zn
exceeds a prescribed bound. If it remains below the bound within maxit
iterations, we assume the sequence to be bounded.
Note how, in the following piece of code,meshgrid
is used to generate a matrix of complex parameter values c:
def mandelbrot(h,w, maxit=20): X,Y = meshgrid(linspace(-2, 0.8, w), linspace(-1.4, 1.4, h)) c = X + Y*1j z = c exceeds = zeros(z.shape, dtype=bool) for iteration in range(maxit): z = z**2 + c exceeded = abs(z) > 4 exceeds_now = exceeded & (logical_not(exceeds)) ...