{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": 2, "cell_type": "code", "source": [], "outputs": [], "metadata": { "collapsed": true } }, { "source": [ "# Bringing it all Together - Animations in Matplotlib" ], "cell_type": "markdown", "metadata": { "ein.tags": [ "worksheet-0" ] } }, { "source": [ "In this final exercise we are going to make use of all the skills you have developed in Python today, and approximate pi with a nice matplotlib animation.\n", "\n", "In this example we are going to approximate pi by calculating the difference in the area of a unit square and a unit circle. We shall do this by randomly picking a point inside the unit square and determining if that point is inside the unit circle contained within the square. The ratio of points inside both the square and the circle to the ratio of points only in the square will allow us to approximate pi.\n", "\n", "First we will introduce you to the matplotlib animation module." ], "cell_type": "markdown", "metadata": { "ein.tags": [ "worksheet-0" ] } }, { "source": [ "# Challenge 1\n", "\n", "Approximate pi by the Monte Carlo method in a for loop. For each iteration of the loop you will need to generate a random x and y coordinate of your point (between 0 and 1), and then calculate if this is inside the circule of radius 1. You can then approximate $\\pi$ using the following equation:\n", "\n", "$$\n", "\\pi = \\frac{\\text{number of points in circle}}{\\text{total number of points}}\n", "$$\n", "\n", "You will probably find [`numpy.random.rand()`](http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.random.rand.html) and [`numpy.sqrt()`](http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.sqrt.html) useful.\n", "\n", "Print the approximation at each step to see it getting more accurate." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 3, "cell_type": "code", "source": [], "outputs": [], "metadata": { "collapsed": true } }, { "execution_count": 10, "cell_type": "code", "source": [], "outputs": [], "metadata": { "scrolled": true, "collapsed": false } }, { "source": [ "## Animations" ], "cell_type": "markdown", "metadata": { "ein.tags": [ "worksheet-0" ] } }, { "source": [ "Matplotlib supports animation of all of its figure types. We will show you an example here.\n", "\n", "First things first, we need to import Animation." ], "cell_type": "markdown", "metadata": { "ein.tags": [ "worksheet-0" ] } }, { "execution_count": 1, "cell_type": "code", "source": [], "outputs": [], "metadata": { "autoscroll": "json-false", "collapsed": false, "ein.tags": [ "worksheet-0" ] } }, { "source": [ "So animation plotting is based off creating a function. So in this case, we are animating a line plot." ], "cell_type": "markdown", "metadata": { "ein.tags": [ "worksheet-0" ] } }, { "execution_count": 2, "cell_type": "code", "source": [], "outputs": [], "metadata": { "autoscroll": "json-false", "collapsed": false, "ein.tags": [ "worksheet-0" ] } }, { "execution_count": 4, "cell_type": "code", "source": [], "outputs": [], "metadata": { "autoscroll": "json-false", "collapsed": false, "ein.tags": [ "worksheet-0" ] } }, { "execution_count": 5, "cell_type": "code", "source": [], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 6, "cell_type": "code", "source": [], "outputs": [], "metadata": { "collapsed": true } }, { "source": [ "This works! Animating a 2D image is similar. Except in the animate function, you will set both x and y data." ], "cell_type": "markdown", "metadata": { "ein.tags": [ "worksheet-0" ] } }, { "source": [ "# Challenge 2\n", "\n", "Modify your $\\pi$ calculation to animate as it calculates pi. \n", "\n", "You can plot the circle of the line using the equation:\n", "$$\n", "x^2 + y^2 = 1\n", "$$\n", "\n", "Set the title of your plot to be the running approximation of $\\pi$." ], "cell_type": "markdown", "metadata": {} }, { "execution_count": 7, "cell_type": "code", "source": [], "outputs": [], "metadata": { "autoscroll": "json-false", "collapsed": false, "ein.tags": [ "worksheet-0" ] } }, { "execution_count": 8, "cell_type": "code", "source": [], "outputs": [], "metadata": { "collapsed": false } }, { "execution_count": 11, "cell_type": "code", "source": [], "outputs": [], "metadata": { "autoscroll": "json-false", "collapsed": false, "ein.tags": [ "worksheet-0" ] } }, { "execution_count": 10, "cell_type": "code", "source": [], "outputs": [], "metadata": { "autoscroll": "json-false", "collapsed": false, "ein.tags": [ "worksheet-0" ] } }, { "source": [ "There is a further IPython Notebook talking about this in more detail." ], "cell_type": "markdown", "metadata": { "ein.tags": [ "worksheet-0" ] } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "name": "python3", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "file_extension": ".py", "version": "3.5.1", "pygments_lexer": "ipython3", "codemirror_mode": { "version": 3, "name": "ipython" } } } }