forked from doingmathwithpython/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmandelbrot.py
More file actions
59 lines (48 loc) · 1.47 KB
/
mandelbrot.py
File metadata and controls
59 lines (48 loc) · 1.47 KB
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
'''
mandelbrot.py
Draw a Mandelbrot set
Using "Escape time algorithm" from:
http://en.wikipedia.org/wiki/Mandelbrot_set#Computer_drawings
Thanks to http://www.vallis.org/salon/summary-10.html for some important
ideas for implementation.
'''
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# Subset of the complex plane we are considering
x0, x1 = -2.5, 1
y0, y1 = -1, 1
def initialize_image(x_p, y_p):
image = []
for i in range(y_p):
x_colors = []
for j in range(x_p):
x_colors.append(0)
image.append(x_colors)
return image
def mandelbrot_set():
# Number of divisions along each axis
n = 400
# Maximum iterations
max_iteration=1000
image = initialize_image(n, n)
# Generate a set of equally spaced points in the region
# above
dx = (x1-x0)/(n-1)
dy = (y1-y0)/(n-1)
x_coords = [x0 + i*dx for i in range(n)]
y_coords = [y0 + i*dy for i in range(n)]
for i, x in enumerate(x_coords):
for k, y in enumerate(y_coords):
z1 = complex(0, 0)
iteration = 0
c = complex(x, y)
while (abs(z1) < 2 and iteration < max_iteration):
z1 = z1**2 + c
iteration += 1
image[k][i] = iteration
return image
if __name__ == '__main__':
image = mandelbrot_set()
plt.imshow(image, origin='lower', extent=(x0, x1, y0,y1),
cmap=cm.Greys_r, interpolation='nearest')
plt.show()