Jump to content

Mandelbrot set: Difference between revisions

m (→‎Mercator zooms of the Mandelbrot set using NumPy: Image slightly enlarged so that the coordinates do not overlap)
Line 7,147:
 
===Mercator zooms of the Mandelbrot set using NumPy===
It is possible to print the Mandelbrot set with a scatter plot.
<lang python>import numpy as np
import matplotlib.pyplot as plt
 
d, n = 20, 50 # pixel density and number of iterations
r = 2.5 # escape radius (must be greater than 2)
 
x = np.linspace(-2.5, 1.5, 4 * d + 1)
y = np.linspace(-1.5, 1.5, 3 * d + 1)
 
A, B = np.meshgrid(x, y)
C = A + B * 1j
 
Z = np.zeros_like(C)
T = np.zeros(C.shape)
 
for k in range(n):
M = abs(Z) < r
Z[M] = Z[M] ** 2 + C[M]
T[M] = k + 1
 
fig, ax = plt.subplots(figsize=(8, 6))
ax.imshow(T, cmap=plt.cm.twilight_shifted, vmin=0, vmax=n)
plt.savefig("Mandelbrot_image.png", dpi=100)
 
D = 1 / d * np.ones(C.shape)
S = (100 * D) ** 2
 
fig, ax = plt.subplots(figsize=(8, 6))
ax.scatter(A, B, s=S, c=T, cmap=plt.cm.twilight_shifted, vmin=0, vmax=n)
plt.savefig("Mandelbrot_scatterplot.png", dpi=100)</lang>
A small change in the code above allows Mercator zooms of the Mandelbrot set (see [http://www.madore.org/~david/math/mandelbrot.html Mandelbrot set images and videos]).
<lang python>import numpy as np
305

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.