1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| import numpy as np
def mandelbrot_set(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon=2.0): X = np.linspace(xmin, xmax, xn).astype(np.float32) Y = np.linspace(ymin, ymax, yn).astype(np.float32) C = X + Y[:, None] * 1j N = np.zeros_like(C, dtype=int) Z = np.zeros_like(C) for n in range(maxiter): I = np.less(abs(Z), horizon) N[I] = n Z[I] = Z[I]**2 + C[I] N[N == maxiter-1] = 0 return Z, N
if __name__ == '__main__': import time import matplotlib from matplotlib import colors import matplotlib.pyplot as plt
xmin, xmax, xn = -2.25, +0.75, 3000/2 ymin, ymax, yn = -1.25, +1.25, 2500/2 maxiter = 200 horizon = 2.0 ** 40 log_horizon = np.log(np.log(horizon))/np.log(2) Z, N = mandelbrot_set(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon)
with np.errstate(invalid='ignore'): M = np.nan_to_num(N + 1 - np.log(np.log(abs(Z)))/np.log(2) + log_horizon)
dpi = 72 width = 10 height = 10*yn/xn fig = plt.figure(figsize=(width, height), dpi=dpi) ax = fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False, aspect=1)
light = colors.LightSource(azdeg=315, altdeg=10) M = light.shade(M, cmap=plt.cm.hot, vert_exag=1.5, norm=colors.PowerNorm(0.3), blend_mode='hsv') plt.imshow(M, extent=[xmin, xmax, ymin, ymax], interpolation="bicubic") ax.set_xticks([]) ax.set_yticks([])
year = time.strftime("%Y") text = ("The Mandelbrot fractal set\n" "Rendered with matplotlib %s, %s - http://matplotlib.org" % (matplotlib.__version__, year)) ax.text(xmin+.025, ymin+.025, text, color="white", fontsize=12, alpha=0.5)
plt.show()
|