{ "metadata": { "name": "", "signature": "sha256:a8eb9ce664aeb196db816d743579f00eb34134e95d6606d18dbea8d9adfe0759" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Goals\n", "\n", "* learn how to use matplotlib and some other plotting libraries\n", "* learn different modes for learning matplotlib ([Usage \u2014 Matplotlib 1.3.1 documentation](http://matplotlib.org/faq/usage_faq.html)):\n", "\n", "
\n", "

\n", "The purpose of a plotting package is to assist you in visualizing your data as easily as possible, with all the necessary control \u2013 that is, by using relatively high-level commands most of the time, and still have the ability to use the low-level commands when needed.\n", "

\n", "

Therefore, everything in matplotlib is organized in a hierarchy.

\n", "
    \n", "
  1. \n", "At the top of the hierarchy is the matplotlib \u201cstate-machine environment\u201d which is provided by the matplotlib.pyplot module. At this level, simple functions are used to add plot elements (lines, images, text, etc.) to the current axes in the current figure.\n", "
  2. \n", "
  3. \n", "The next level down in the hierarchy is the first level of the object-oriented interface, in which pyplot is used only for a few functions such as figure creation, and the user explicitly creates and keeps track of the figure and axes objects. At this level, the user uses pyplot to create figures, and through those figures, one or more axes objects can be created. These axes objects are then used for most plotting actions.\n", "
  4. \n", "
  5. \n", "For even more control \u2013 which is essential for things like embedding matplotlib plots in GUI applications \u2013 the pyplot level may be dropped completely, leaving a purely object-oriented approach.\n", "
  6. \n", "
\n", "
\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matplotlib is the foundational library for 2d-plotting in the Python world.\n", "\n", "[Matplotlib and the Future of Visualization in Python](http://jakevdp.github.io/blog/2013/03/23/matplotlib-and-the-future-of-visualization-in-python/)\n", "\n", "integration of mpl with Pandas is very useful: [Plotting with matplotlib \u2014 pandas 0.13.1 documentation](http://pandas.pydata.org/pandas-docs/stable/visualization.html)\n", "\n", "[matplotlib/matplotlib](https://github.com/matplotlib/matplotlib)\n", "\n", "look at beautifiers:\n", "\n", "* [olgabot/prettyplotlib](https://github.com/olgabot/prettyplotlib)\n", "* [Controlling figure aesthetics in seaborn \u2014 seaborn 0.2.1 documentation](http://www.stanford.edu/~mwaskom/software/seaborn/aesthetics.html)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline \n", "\n", "import numpy as np\n", "import pandas as pd\n", "from pandas import DataFrame, Series\n", "\n", "import matplotlib.pyplot as plt\n", "\n", "plt.plot(np.arange(10))\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import matplotlib.pyplot as plt\n", "ca = plt.gca()\n", "ca.set_xlabel(\"hello\")\n", "lines = plt.plot([1,2,3,4], 'r')\n", "label = plt.ylabel('some numbers')\n", "plt.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "line= lines[0]\n", "line.get_color()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Usage \u2014 Matplotlib 1.3.1 documentation](http://matplotlib.org/faq/usage_faq.html#matplotlib-pylab-and-pyplot-how-are-they-related)\n", "\n", ">Pyplot provides the state-machine interface to the underlying plotting library in matplotlib. This means that figures and axes are implicitly and automatically created to achieve the desired plot. For example, calling plot from pyplot will automatically create the necessary figure and axes to achieve the desired plot. Setting a title will then automatically set that title to the current axes object" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import pandas as pd\n", "from pandas import DataFrame, Series\n", "\n", "# using the pyplot interface\n", "\n", "import matplotlib.pyplot as plt" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# plot different types of graphs to show distribution of populations\n", "# Chap 8 of PfDA\n", "# http://my.safaribooksonline.com/book/programming/python/9781449323592/8dot-plotting-and-visualization/id2802076\n", "\n", "# hello world of mpl\n", "\n", "plt.plot(np.arange(10))\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = np.arange(0.001, 1, 0.001)\n", "y = np.apply_along_axis(lambda x:-x*np.log(x),0,np.arange(0.001, 1, 0.001))\n", "plt.plot(x,y)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = np.arange(0, 10, 0.2)\n", "y = np.sin(x)\n", "fig = plt.figure()\n", "ax = fig.add_subplot(111)\n", "ax.plot(x, y)\n", "# plt.show() done implicitly\n", "#plt.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = np.arange(0.001, 1, 0.001)\n", "y = np.apply_along_axis(lambda x:-(x*np.log(x)+(1-x)*np.log(1-x)),0,np.arange(0.001, 1, 0.001))\n", "plt.plot(x,y)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.plot(np.random.random_sample(10000), 'o', ms=1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "fig = plt.figure()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# set up a 2x2 grid of subplots and instantiate 3 of them\n", "\n", "ax1 = fig.add_subplot(2, 2, 1)\n", "ax2 = fig.add_subplot(2, 2, 2)\n", "ax3 = fig.add_subplot(2, 2, 3)\n", "\n", "fig" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "PfDA: \n", "\n", "
When you issue a plotting command like plt.plot([1.5, 3.5, -2, 1.6]), matplotlib draws on the last figure and subplot used (creating one if necessary), thus hiding the figure and subplot creation
" ] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.plot([1.5, 3.5, -2, 1.6])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "options = [None, 'k--', 'ro', 'g+']\n", "\n", "fig = plt.figure()\n", "\n", "# let's plot subplot 2 columns wide\n", "# try different options\n", "\n", "num_row = math.ceil(len(options)/2.0)\n", "\n", "for (i, option) in enumerate(options):\n", " ax = fig.add_subplot(num_row,2, i+1)\n", " \n", " if option is not None:\n", " ax.plot([1.5, 3.5, -2, 1.6], option)\n", " else:\n", " ax.plot([1.5, 3.5, -2, 1.6])\n", "\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from numpy.random import randn\n", "\n", "fig = plt.figure()\n", "\n", "ax1 = fig.add_subplot(2, 2, 1)\n", "ax2 = fig.add_subplot(2, 2, 2)\n", "ax3 = fig.add_subplot(2, 2, 3)\n", "\n", "ax1.hist(randn(100), bins=20, color='k', alpha=0.3)\n", "ax1.set_xlabel('ax1')\n", "ax2.scatter(np.arange(30), np.arange(30) + 3 * randn(30))\n", "ax3.plot(randn(50).cumsum(), 'k--')\n", "\n", "\n", "fig.tight_layout()\n", "fig.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "#http://matplotlib.org/examples/text_labels_and_annotations/unicode_demo.html\n", "from __future__ import unicode_literals\n", "\n", "import matplotlib.pyplot as plt\n", "\n", "\n", "plt.title('D\u00e9velopp\u00e9s et fabriqu\u00e9s')\n", "plt.xlabel(\"r\u00e9activit\u00e9 nous permettent d'\u00eatre s\u00e9lectionn\u00e9s et adopt\u00e9s\")\n", "plt.ylabel('Andr\u00e9 was here!')\n", "plt.text( 0.2, 0.8, 'Institut f\u00fcr Festk\u00f6rperphysik', rotation=45)\n", "plt.text( 0.4, 0.2, 'AVA (check kerning)')\n", "\n", "plt.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "#http://matplotlib.org/examples/api/barchart_demo.html\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "N = 5\n", "menMeans = (20, 35, 30, 35, 27)\n", "menStd = (2, 3, 4, 1, 2)\n", "\n", "ind = np.arange(N) # the x locations for the groups\n", "width = 0.35 # the width of the bars\n", "\n", "fig, ax = plt.subplots()\n", "rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)\n", "\n", "womenMeans = (25, 32, 34, 20, 25)\n", "womenStd = (3, 5, 2, 3, 3)\n", "rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)\n", "\n", "# add some\n", "ax.set_ylabel('Scores')\n", "ax.set_title('Scores by group and gender')\n", "ax.set_xticks(ind+width)\n", "ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') )\n", "\n", "ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )\n", "\n", "def autolabel(rects):\n", " # attach some text labels\n", " for rect in rects:\n", " height = rect.get_height()\n", " ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),\n", " ha='center', va='bottom')\n", "\n", "autolabel(rects1)\n", "autolabel(rects2)\n", "\n", "plt.show()\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "World" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "going back to our calculation from [Day_01_B_World_Population.ipynb](http://nbviewer.ipython.org/github/rdhyee/working-open-data-2014/blob/master/notebooks/Day_01_B_World_Population.ipynb)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# https://gist.github.com/rdhyee/8511607/raw/f16257434352916574473e63612fcea55a0c1b1c/population_of_countries.json\n", "# scraping of https://en.wikipedia.org/w/index.php?title=List_of_countries_by_population_(United_Nations)&oldid=590438477\n", "\n", "# read population in\n", "import json\n", "import requests\n", "\n", "pop_json_url = \"https://gist.github.com/rdhyee/8511607/raw/f16257434352916574473e63612fcea55a0c1b1c/population_of_countries.json\"\n", "pop_list= requests.get(pop_json_url).json()\n", "pop_list" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "country_num = range(len(pop_list))\n", "country_names = [c[1] for c in pop_list]\n", "country_pops = [int(c[2]) for c in pop_list]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.plot(country_pops)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from itertools import izip, islice\n", "sampled_country_tuples = list(islice(izip(country_num, country_names),0,None,10))\n", "sampled_i = [s[0] for s in sampled_country_tuples]\n", "sampled_countries = [s[1] for s in sampled_country_tuples]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# bar charts\n", "# can find barh: http://matplotlib.org/examples/lines_bars_and_markers/barh_demo.html\n", "# http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.bar\n", "\n", "plt.bar(left=map(lambda x: x, range(len(country_pops))),height=country_pops, width=2, log='x')\n", "#plt.xticks(range(len(country_pops)), country_names, rotation='vertical')\n", "plt.xticks(sampled_i, sampled_countries, rotation='vertical')\n", "plt.ylabel('Population')\n", "plt.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# what if we make a DataFrame from pop_list\n", "df = DataFrame(pop_list)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.plot(df[2])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.plot(df[2].cumsum())" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }