{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Matplotlib unchained\n\nComparative path demonstration of frequency from a fake signal of a pulsar\n(mostly known because of the cover for Joy Division's Unknown Pleasures).\n\nAuthor: Nicolas P. Rougier\n\nOutput generated via `matplotlib.animation.Animation.to_jshtml`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.animation as animation\n\n# Fixing random state for reproducibility\nnp.random.seed(19680801)\n\n\n# Create new Figure with black background\nfig = plt.figure(figsize=(8, 8), facecolor='black')\n\n# Add a subplot with no frame\nax = plt.subplot(frameon=False)\n\n# Generate random data\ndata = np.random.uniform(0, 1, (64, 75))\nX = np.linspace(-1, 1, data.shape[-1])\nG = 1.5 * np.exp(-4 * X ** 2)\n\n# Generate line plots\nlines = []\nfor i in range(len(data)):\n # Small reduction of the X extents to get a cheap perspective effect\n xscale = 1 - i / 200.\n # Same for linewidth (thicker strokes on bottom)\n lw = 1.5 - i / 100.0\n line, = ax.plot(xscale * X, i + G * data[i], color=\"w\", lw=lw)\n lines.append(line)\n\n# Set y limit (or first line is cropped because of thickness)\nax.set_ylim(-1, 70)\n\n# No ticks\nax.set_xticks([])\nax.set_yticks([])\n\n# 2 part titles to get different font weights\nax.text(0.5, 1.0, \"MATPLOTLIB \", transform=ax.transAxes,\n ha=\"right\", va=\"bottom\", color=\"w\",\n family=\"sans-serif\", fontweight=\"light\", fontsize=16)\nax.text(0.5, 1.0, \"UNCHAINED\", transform=ax.transAxes,\n ha=\"left\", va=\"bottom\", color=\"w\",\n family=\"sans-serif\", fontweight=\"bold\", fontsize=16)\n\n\ndef update(*args):\n # Shift all data to the right\n data[:, 1:] = data[:, :-1]\n\n # Fill-in new values\n data[:, 0] = np.random.uniform(0, 1, len(data))\n\n # Update data\n for i in range(len(data)):\n lines[i].set_ydata(i + G * data[i])\n\n # Return modified artists\n return lines\n\n# Construct the animation, using the update function as the animation director.\nanim = animation.FuncAnimation(fig, update, interval=10, save_count=100)\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }