MATPLOTLIB UNCHAINED

脉冲星的假信号频率的比较路径演示(主要是因为Joy Division的未知乐趣的封面而闻名)。

作者:Nicolas P. Rougier

MATPLOTLIB UNCHAINED示例

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

下载这个示例