|
| 1 | +""" |
| 2 | +=================================== |
| 3 | +(Infinitly) Zoomable Mandelbrot Set |
| 4 | +=================================== |
| 5 | +
|
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import matplotlib.pyplot as plt |
| 10 | +import numpy as np |
| 11 | + |
| 12 | +from data_prototype.wrappers import ImageWrapper |
| 13 | +from data_prototype.containers import FuncContainer |
| 14 | + |
| 15 | +from matplotlib.colors import Normalize |
| 16 | + |
| 17 | +maxiter = 75 |
| 18 | + |
| 19 | + |
| 20 | +def mandelbrot_set(X, Y, maxiter, *, horizon=3, power=2): |
| 21 | + C = X + Y[:, None] * 1j |
| 22 | + N = np.zeros_like(C, dtype=int) |
| 23 | + Z = np.zeros_like(C) |
| 24 | + for n in range(maxiter): |
| 25 | + I = abs(Z) < horizon |
| 26 | + N += I |
| 27 | + Z[I] = Z[I] ** power + C[I] |
| 28 | + N[N == maxiter] = -1 |
| 29 | + return Z, N |
| 30 | + |
| 31 | + |
| 32 | +fc = FuncContainer( |
| 33 | + {}, |
| 34 | + xyfuncs={ |
| 35 | + "xextent": ((2,), lambda x, y: [x[0], x[-1]]), |
| 36 | + "yextent": ((2,), lambda x, y: [y[0], y[-1]]), |
| 37 | + "image": (("N", "M"), lambda x, y: mandelbrot_set(x, y, maxiter)[1]), |
| 38 | + }, |
| 39 | +) |
| 40 | +cmap = plt.get_cmap() |
| 41 | +cmap.set_under("w") |
| 42 | +im = ImageWrapper(fc, norm=Normalize(0, maxiter), cmap=cmap) |
| 43 | + |
| 44 | +fig, ax = plt.subplots() |
| 45 | +ax.add_artist(im) |
| 46 | +ax.set_xlim(-1, 1) |
| 47 | +ax.set_ylim(-1, 1) |
| 48 | +ax.set_aspect("equal") |
| 49 | +fig.colorbar(im) |
| 50 | +plt.show() |
0 commit comments