{ "metadata": { "name": "", "signature": "sha256:e9fa07b73751098654e37c87f10598f24102fbca4ab2f9ff1e7a9236b8e9730f" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Small multiples" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "in Plotly" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From Wikipedia:\n", "\n", ">A small multiple (sometimes called trellis chart, lattice chart, grid chart, or panel chart) is a series or grid of small similar graphics or charts, allowing them to be easily compared. The term was popularized by Edward Tufte. \n", "\n", "In this notebook, we take data from the GapMinder dataset of gross domestic product (GDP) of all the world's countries (the same dataset that Hans Rosling used to make his famous bubble charts) to plot a GDP per capita small multiple with one subplot per country for **all** of the world's country.\n", "\n", "That's right, Plotly can do that!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, import the Plotly modules and graph object required for this task:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import plotly.plotly as py # signing in with your credentials file\n", "import plotly.tools as tls" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "from plotly.graph_objs import Figure, Data, Layout\n", "from plotly.graph_objs import Scatter\n", "from plotly.graph_objs import Marker, Font\n", "from plotly.graph_objs import XAxis, YAxis, Annotation, Annotations" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, import the data into pandas dataframe and import numpy:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import pandas as pd\n", "import urllib2\n", "\n", "# The datasets' url. Thanks Jennifer Bryan!\n", "url_csv = 'http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/\\\n", "gapminder/data/gapminderDataFiveYear.txt'\n", "\n", "file_csv = urllib2.urlopen(url_csv) # import csv file into this session\n", "df = pd.read_csv(file_csv, sep='\\t') # load csv file into a dataframe" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "df.head() # print the first 5 lines of the dataframe" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", " | country | \n", "year | \n", "pop | \n", "continent | \n", "lifeExp | \n", "gdpPercap | \n", "
---|---|---|---|---|---|---|
0 | \n", "Afghanistan | \n", "1952 | \n", "8425333 | \n", "Asia | \n", "28.801 | \n", "779.445314 | \n", "
1 | \n", "Afghanistan | \n", "1957 | \n", "9240934 | \n", "Asia | \n", "30.332 | \n", "820.853030 | \n", "
2 | \n", "Afghanistan | \n", "1962 | \n", "10267083 | \n", "Asia | \n", "31.997 | \n", "853.100710 | \n", "
3 | \n", "Afghanistan | \n", "1967 | \n", "11537966 | \n", "Asia | \n", "34.020 | \n", "836.197138 | \n", "
4 | \n", "Afghanistan | \n", "1972 | \n", "13079460 | \n", "Asia | \n", "36.088 | \n", "739.981106 | \n", "
5 rows \u00d7 6 columns
\n", "