| | """ |
| | =============== |
| | Rain simulation |
| | =============== |
| | |
| | Simulates rain drops on a surface by animating the scale and opacity |
| | of 50 scatter points. |
| | |
| | Author: Nicolas P. Rougier |
| | |
| | Output generated via `matplotlib.animation.Animation.to_jshtml`. |
| | """ |
| |
|
| | import matplotlib.pyplot as plt |
| | import numpy as np |
| |
|
| | from matplotlib.animation import FuncAnimation |
| |
|
| | |
| | np.random.seed(19680801) |
| |
|
| |
|
| | |
| | fig = plt.figure(figsize=(7, 7)) |
| | ax = fig.add_axes([0, 0, 1, 1], frameon=False) |
| | ax.set_xlim(0, 1), ax.set_xticks([]) |
| | ax.set_ylim(0, 1), ax.set_yticks([]) |
| |
|
| | |
| | n_drops = 50 |
| | rain_drops = np.zeros(n_drops, dtype=[('position', float, (2,)), |
| | ('size', float), |
| | ('growth', float), |
| | ('color', float, (4,))]) |
| |
|
| | |
| | |
| | rain_drops['position'] = np.random.uniform(0, 1, (n_drops, 2)) |
| | rain_drops['growth'] = np.random.uniform(50, 200, n_drops) |
| |
|
| | |
| | |
| | scat = ax.scatter(rain_drops['position'][:, 0], rain_drops['position'][:, 1], |
| | s=rain_drops['size'], lw=0.5, edgecolors=rain_drops['color'], |
| | facecolors='none') |
| |
|
| |
|
| | def update(frame_number): |
| | |
| | current_index = frame_number % n_drops |
| |
|
| | |
| | rain_drops['color'][:, 3] -= 1.0/len(rain_drops) |
| | rain_drops['color'][:, 3] = np.clip(rain_drops['color'][:, 3], 0, 1) |
| |
|
| | |
| | rain_drops['size'] += rain_drops['growth'] |
| |
|
| | |
| | |
| | rain_drops['position'][current_index] = np.random.uniform(0, 1, 2) |
| | rain_drops['size'][current_index] = 5 |
| | rain_drops['color'][current_index] = (0, 0, 0, 1) |
| | rain_drops['growth'][current_index] = np.random.uniform(50, 200) |
| |
|
| | |
| | scat.set_edgecolors(rain_drops['color']) |
| | scat.set_sizes(rain_drops['size']) |
| | scat.set_offsets(rain_drops['position']) |
| |
|
| |
|
| | |
| | animation = FuncAnimation(fig, update, interval=10, save_count=100) |
| | plt.show() |
| |
|