|
| 1 | +""" |
| 2 | +Comparative path demonstration of frequency from a fake signal of a pulsar. |
| 3 | +(mostly known because of the cover for Joy Division's Unknown Pleasures) |
| 4 | +
|
| 5 | +Author: Nicolas P. Rougier |
| 6 | +""" |
| 7 | +import numpy as np |
| 8 | +import matplotlib.pyplot as plt |
| 9 | +import matplotlib.animation as animation |
| 10 | + |
| 11 | +# Create new Figure with black background |
| 12 | +fig = plt.figure(figsize=(8, 8), facecolor='black') |
| 13 | + |
| 14 | +# Add a subplot with no frame |
| 15 | +ax = plt.subplot(111, frameon=False) |
| 16 | + |
| 17 | +# Generate random data |
| 18 | +data = np.random.uniform(0, 1, (64, 75)) |
| 19 | +X = np.linspace(-1, 1, data.shape[-1]) |
| 20 | +G = 1.5 * np.exp(-4 * X * X) |
| 21 | + |
| 22 | +# Generate line plots |
| 23 | +lines = [] |
| 24 | +for i in range(len(data)): |
| 25 | + # Small reduction of the X extents to get a cheap perspective effect |
| 26 | + xscale = 1 - i / 200. |
| 27 | + # Same for linewidth (thicker strokes on bottom) |
| 28 | + lw = 1.5 - i / 100.0 |
| 29 | + line, = ax.plot(xscale * X, i + G * data[i], color="w", lw=lw) |
| 30 | + lines.append(line) |
| 31 | + |
| 32 | +# Set y limit (or first line is cropped because of thickness) |
| 33 | +ax.set_ylim(-1, 70) |
| 34 | + |
| 35 | +# No ticks |
| 36 | +ax.set_xticks([]) |
| 37 | +ax.set_yticks([]) |
| 38 | + |
| 39 | +# 2 part titles to get different font weights |
| 40 | +ax.text(0.5, 1.0, "MATPLOTLIB ", transform=ax.transAxes, |
| 41 | + ha="right", va="bottom", color="w", |
| 42 | + family="sans-serif", fontweight="light", fontsize=16) |
| 43 | +ax.text(0.5, 1.0, "UNCHAINED", transform=ax.transAxes, |
| 44 | + ha="left", va="bottom", color="w", |
| 45 | + family="sans-serif", fontweight="bold", fontsize=16) |
| 46 | + |
| 47 | +# Update function |
| 48 | +def update(*args): |
| 49 | + # Shift all data to the right |
| 50 | + data[:, 1:] = data[:, :-1] |
| 51 | + |
| 52 | + # Fill-in new values |
| 53 | + data[:, 0] = np.random.uniform(0, 1, len(data)) |
| 54 | + |
| 55 | + # Update data |
| 56 | + for i in range(len(data)): |
| 57 | + lines[i].set_ydata(i + G * data[i]) |
| 58 | + |
| 59 | + # Return modified artists |
| 60 | + return lines |
| 61 | + |
| 62 | +# Construct the animation, using the update function as the animation |
| 63 | +# director. |
| 64 | +anim = animation.FuncAnimation(fig, update, interval=10) |
| 65 | +plt.show() |
0 commit comments