Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3228926

Browse files
committed
ADD: rain demo.py
1 parent ef462e4 commit 3228926

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

examples/animation/rain.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -----------------------------------------------------------------------------
2+
# Rain simulation
3+
# Author: Nicolas P. Rougier
4+
# -----------------------------------------------------------------------------
5+
import matplotlib
6+
import numpy as np
7+
matplotlib.use('TkAgg')
8+
matplotlib.rcParams['toolbar'] = 'None'
9+
import matplotlib.pyplot as plt
10+
from matplotlib.animation import FuncAnimation
11+
12+
13+
fig = plt.figure(figsize=(8,8))
14+
fig.patch.set_facecolor('white')
15+
ax = fig.add_axes([0,0,1,1], frameon=False, aspect=1)
16+
17+
P = np.zeros(50, dtype=[('position', float, 2),
18+
('size', float, 1),
19+
('growth', float, 1),
20+
('color', float, 4)])
21+
scat = ax.scatter(P['position'][:,0], P['position'][:,1], P['size'], lw=0.5,
22+
animated=True, edgecolors = P['color'], facecolors='None')
23+
ax.set_xlim(0,1), ax.set_xticks([])
24+
ax.set_ylim(0,1), ax.set_yticks([])
25+
26+
27+
def update(frame):
28+
i = frame % 50
29+
30+
# Make all colors more transparent
31+
P['color'][:,3] = np.maximum(0, P['color'][:,3] - 1.0/len(P))
32+
# Make all circles bigger
33+
P['size'] += P['growth']
34+
35+
# Pick new position for oldest rain drop
36+
P['position'][i] = np.random.uniform(0,1,2)
37+
P['size'][i] = 5
38+
P['color'][i] = 0,0,0,1
39+
P['growth'][i] = np.random.uniform(50,200)
40+
41+
# Update scatter plots
42+
scat.set_edgecolors(P['color'])
43+
scat.set_sizes(P['size'])
44+
scat.set_offsets(P['position'])
45+
46+
return scat,
47+
48+
animation = FuncAnimation(fig, update, interval=10, blit=True)
49+
plt.show()

0 commit comments

Comments
 (0)