{ "metadata": { "name": "Central Limit Theroem" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "from pandas import *\n", "from matplotlib import animation\n", "import numpy as np " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "sys.path.append('C:\\Python27\\Lib\\site-packages\\JSAnimation')\n", "from JSAnimation.IPython_display import display_animation" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Histogram animation" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"\n", "This example shows how to use a path patch to draw a bunch of\n", "rectangles for an animated histogram\n", "\"\"\"\n", "from JSAnimation import IPython_display\n", "import numpy as np\n", "\n", "import matplotlib.pyplot as plt\n", "import matplotlib.patches as patches\n", "import matplotlib.path as path\n", "import matplotlib.animation as animation\n", "\n", "fig = plt.figure()\n", "ax = fig.add_subplot(111)\n", "\n", "# histogram our data with numpy\n", "data = np.random.randn(1000)\n", "n, bins = np.histogram(data, 100)\n", "\n", "# get the corners of the rectangles for the histogram\n", "left = np.array(bins[:-1])\n", "right = np.array(bins[1:])\n", "bottom = np.zeros(len(left))\n", "top = bottom + n\n", "nrects = len(left)\n", "\n", "# here comes the tricky part -- we have to set up the vertex and path\n", "# codes arrays using moveto, lineto and closepoly\n", "\n", "# for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the\n", "# CLOSEPOLY; the vert for the closepoly is ignored but we still need\n", "# it to keep the codes aligned with the vertices\n", "nverts = nrects*(1+3+1)\n", "verts = np.zeros((nverts, 2))\n", "codes = np.ones(nverts, int) * path.Path.LINETO\n", "codes[0::5] = path.Path.MOVETO\n", "codes[4::5] = path.Path.CLOSEPOLY\n", "verts[0::5,0] = left\n", "verts[0::5,1] = bottom\n", "verts[1::5,0] = left\n", "verts[1::5,1] = top\n", "verts[2::5,0] = right\n", "verts[2::5,1] = top\n", "verts[3::5,0] = right\n", "verts[3::5,1] = bottom\n", "\n", "barpath = path.Path(verts, codes)\n", "patch = patches.PathPatch(barpath, facecolor='green', edgecolor='yellow', alpha=0.5)\n", "ax.add_patch(patch)\n", "\n", "ax.set_xlim(left[0], right[-1])\n", "ax.set_ylim(bottom.min(), top.max())\n", "\n", "def animate(i):\n", " # simulate new data coming in\n", " data = np.random.randn(1000)\n", " n, bins = np.histogram(data, 100)\n", " top = bottom + n\n", " verts[1::5,1] = top\n", " verts[2::5,1] = top\n", "\n", "ani = animation.FuncAnimation(fig, animate, 100, repeat=False)\n", "display_animation(ani, default_mode='once')" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "