{ "metadata": { "name": "", "signature": "sha256:30266c5857592dae2924586fea59e5cc5ae5b099e6244be5bf19e5abfca26ea9" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "The goal I set out to do: rewrite the [Choropleth](http://bl.ocks.org/mbostock/4060606) d3.js example to work in the IPython notebook. For future work: once we are able to reproduce the Choropleth example, then work to feed the map arbitrary county-level data. \n", "\n", "The first thing I did was to make sure I could get \n", "\n", " \n", "\n", "to work by copying the source to\n", "\n", " \n", "\n", "and serving `us.json` and `unemployment.tsv` from my server with [CORS enabled](http://enable-cors.org/) for these two files:\n", "\n", " * \n", " * \n", " \n", "With the map working on standalone HTML page, then I turned to embedding the map inside an IPython notebook. That's where it got really interesting!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# print out the version of IPython used\n", "import IPython\n", "IPython.version_info" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "(3, 0, 0, 'dev')" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "%%javascript\n", "// https://github.com/mbostock/d3/issues/1693\n", "require.config({\n", " paths: {\n", " d3: \"http://d3js.org/d3.v3.min\",\n", " queue: \"http://d3js.org/queue.v1.min\",\n", " topojson: \"http://d3js.org/topojson.v1.min\"\n", " }\n", "});\n", "\n", "require([\"d3\", \"queue\", \"topojson\"], function(d3, queue, topojson) {\n", " console.log(d3.version);\n", " console.log(queue.version);\n", " console.log(topojson.version);\n", "});" ], "language": "python", "metadata": {}, "outputs": [ { "javascript": [ "// https://github.com/mbostock/d3/issues/1693\n", "require.config({\n", " paths: {\n", " d3: \"http://d3js.org/d3.v3.min\",\n", " queue: \"http://d3js.org/queue.v1.min\",\n", " topojson: \"http://d3js.org/topojson.v1.min\"\n", " }\n", "});\n", "\n", "require([\"d3\", \"queue\", \"topojson\"], function(d3, queue, topojson) {\n", " console.log(d3.version);\n", " console.log(queue.version);\n", " console.log(topojson.version);\n", "});" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "%%html\n", "" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "%%html\n", "
\n", "" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "from census_api_utils import (counties, census_labels, diversity, FINAL_LABELS)\n", "r = list(counties(census_labels()))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "from pandas import DataFrame\n", "\n", "counties_df = DataFrame(r)\n", "counties_df = diversity(counties_df)\n", "counties_df[FINAL_LABELS].head()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
NAMETotalWhiteBlackAsianHispanicOtherp_Whitep_Blackp_Asianp_Hispanicp_Otherentropy5entropy4entropy_ricegini_simpson
0 Autauga County 54571 42154 9595 467 1310 1045 0.772462 0.175826 0.008558 0.024005 0.019149 0.441816 0.453294 0.458294 0.371372
1 Baldwin County 182265 152200 16966 1340 7992 3767 0.835048 0.093084 0.007352 0.043848 0.020668 0.388299 0.386196 0.392968 0.291627
2 Barbour County 27457 12837 12820 107 1387 306 0.467531 0.466912 0.003897 0.050515 0.011145 0.580086 0.636407 0.637309 0.560717
3 Bibb County 22915 17191 5024 22 406 272 0.750207 0.219245 0.000960 0.017718 0.011870 0.421943 0.448712 0.451897 0.388665
4 Blount County 57322 50952 724 115 4626 905 0.888873 0.012630 0.002006 0.080702 0.015788 0.274015 0.263741 0.270876 0.202978
\n", "

5 rows \u00d7 16 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ " NAME Total White Black Asian Hispanic Other p_White \\\n", "0 Autauga County 54571 42154 9595 467 1310 1045 0.772462 \n", "1 Baldwin County 182265 152200 16966 1340 7992 3767 0.835048 \n", "2 Barbour County 27457 12837 12820 107 1387 306 0.467531 \n", "3 Bibb County 22915 17191 5024 22 406 272 0.750207 \n", "4 Blount County 57322 50952 724 115 4626 905 0.888873 \n", "\n", " p_Black p_Asian p_Hispanic p_Other entropy5 entropy4 entropy_rice \\\n", "0 0.175826 0.008558 0.024005 0.019149 0.441816 0.453294 0.458294 \n", "1 0.093084 0.007352 0.043848 0.020668 0.388299 0.386196 0.392968 \n", "2 0.466912 0.003897 0.050515 0.011145 0.580086 0.636407 0.637309 \n", "3 0.219245 0.000960 0.017718 0.011870 0.421943 0.448712 0.451897 \n", "4 0.012630 0.002006 0.080702 0.015788 0.274015 0.263741 0.270876 \n", "\n", " gini_simpson \n", "0 0.371372 \n", "1 0.291627 \n", "2 0.560717 \n", "3 0.388665 \n", "4 0.202978 \n", "\n", "[5 rows x 16 columns]" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "counties_df['fips_for_map'] = counties_df.apply(lambda s: str(int(s['state']))+s['county'], axis=1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "df = DataFrame()\n", "df[['id','diversity']] = counties_df[['fips_for_map', 'entropy5']]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "df.to_csv('temp/entropy5.tsv', sep=\"\\t\", index=False)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "!head temp/entropy5.tsv" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "id\tdiversity\r\n", "1001\t0.4418158563234724\r\n", "1003\t0.3882988721091073\r\n", "1005\t0.5800861120053159\r\n", "1007\t0.42194274556423633\r\n", "1009\t0.27401484623038297\r\n", "1011\t0.5101470309826847\r\n", "1013\t0.5120159003143947\r\n", "1015\t0.48169478052525105\r\n", "1017\t0.5139170933218179\r\n" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "%%html\n", "
\n", "" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }