-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbarnsley_fern_animated.py
46 lines (31 loc) · 1.17 KB
/
barnsley_fern_animated.py
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
import os
import imageio as imageio
import matplotlib.pyplot as plt
from barnsley_fern import barnsley_fern
def animate(points):
ax = plt.axes()
ax.clear() # clear axes object
ax.set_xticks([], []) # clear x-axis ticks
ax.set_yticks([], []) # clear y-axis ticks
fractal = barnsley_fern(points, width, height)
img = ax.imshow(fractal[::-1, :], interpolation="bicubic", cmap='viridis')
return img
if __name__ == '__main__':
# canvas width and height
width, height = 1200, 1200
# number of points to plot
max_num_points = width * height
filenames = []
for points in [2_000, 5_000, 10_000, 20_000, 50_000, 100_000, 200_000, 300_000, 400_000, 500_000, 700_000]:
plt.figure(figsize=(10, 10), dpi=300)
animate(points)
filename = f'frame_{points}.jpg'
plt.savefig(filename, bbox_inches='tight')
filenames.append(filename)
plt.close()
with imageio.get_writer('barnsley_fern.gif', mode='I', fps=5) as writer:
for filename in filenames:
image = imageio.imread(filename)
writer.append_data(image)
for filename in set(filenames):
os.remove(filename)