{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Let's try to build a chloropleth map using Bokeh, inspired by [this example](http://bokeh.pydata.org/en/0.10.0/docs/gallery/choropleth.html). Let's get our imports going:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from bokeh.sampledata import us_states, us_counties, unemployment\n", "from bokeh.plotting import figure, show, output_file, output_notebook" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ " \n", "\n", "\n", " \n", "\n", "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It should be noted we're only importing three things from bokeh.plotting: figure, show and output_file." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function figure in module bokeh.plotting:\n", "\n", "figure(**kwargs)\n", " Create a new :class:`~bokeh.plotting.Figure` for plotting, and add it to\n", " the current document.\n", " \n", " Returns:\n", " Figure\n", "\n" ] } ], "source": [ "help(figure)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function show in module bokeh.io:\n", "\n", "show(obj, browser=None, new='tab')\n", " Immediately display a plot object.\n", " \n", " In an IPython/Jupyter notebook, the output is displayed in an output\n", " cell. Otherwise, a browser window or tab is autoraised to display the\n", " plot object.\n", " \n", " If both a server session and notebook output have been configured on\n", " the default output state then the notebook output will be generated to\n", " load the plot from that server session.\n", " \n", " Args:\n", " obj (Widget/Plot object) : a plot object to display\n", " \n", " browser (str, optional) : browser to show with (default: None)\n", " For systems that support it, the **browser** argument allows\n", " specifying which browser to display in, e.g. \"safari\", \"firefox\",\n", " \"opera\", \"windows-default\" (see the ``webbrowser`` module\n", " documentation in the standard lib for more details).\n", " \n", " new (str, optional) : new file output mode (default: \"tab\")\n", " For file-based output, opens or raises the browser window\n", " showing the current output file. If **new** is 'tab', then\n", " opens a new tab. If **new** is 'window', then opens a new window.\n", " \n", " Returns:\n", " None\n", " \n", " .. note::\n", " The ``browser`` and ``new`` parameters are ignored when showing in\n", " an IPython/Jupyter notebook.\n", "\n" ] } ], "source": [ "help(show)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function output_file in module bokeh.io:\n", "\n", "output_file(filename, title='Bokeh Plot', autosave=False, mode='inline', root_dir=None)\n", " Configure the default output state to generate output saved\n", " to a file when :func:`show` is called.\n", " \n", " Args:\n", " filename (str) : a filename for saving the HTML document\n", " \n", " title (str, optional) : a title for the HTML document (default: \"Bokeh Plot\")\n", " \n", " autosave (bool, optional) : whether to automatically save (default: False)\n", " If True, then Bokeh plotting APIs may opt to automatically\n", " save the file more frequently (e.g., after any plotting\n", " command). If False, then the file is only saved upon calling\n", " :func:`show` or :func:`save`.\n", " \n", " mode (str, optional) : how to include BokehJS (default: ``'inline'``)\n", " One of: ``'inline'``, ``'cdn'``, ``'relative(-dev)'`` or\n", " ``'absolute(-dev)'``. See :class:`bokeh.resources.Resources` for more details.\n", " \n", " root_dir (str, optional) : root directory to use for 'absolute' resources. (default: None)\n", " This value is ignored for other resource types, e.g. ``INLINE`` or\n", " ``CDN``.\n", " \n", " Returns:\n", " None\n", " \n", " .. note::\n", " Generally, this should be called at the beginning of an interactive\n", " session or the top of a script.\n", " \n", " .. warning::\n", " This output file will be overwritten on every save, e.g., each time\n", " show() or save() is invoked, or any time a Bokeh plotting API\n", " causes a save, if ``autosave`` is True.\n", "\n" ] } ], "source": [ "help(output_file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now go through the example steps: first, let's load the data." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "us_states = us_states.data.copy()\n", "us_counties = us_counties.data.copy()\n", "unemployment = unemployment.data" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(us_states)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(us_counties)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(unemployment)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now delete Alaska and Hawai from the data and load the coordinates for each state in separate variables." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "del us_states[\"HI\"]\n", "del us_states[\"AK\"]\n", "\n", "state_xs = [us_states[code][\"lons\"] for code in us_states]\n", "state_ys = [us_states[code][\"lats\"] for code in us_states]\n", "\n", "county_xs=[us_counties[code][\"lons\"] for code in us_counties if us_counties[code][\"state\"] not in [\"ak\", \"hi\", \"pr\", \"gu\", \"vi\", \"mp\", \"as\"]]\n", "county_ys=[us_counties[code][\"lats\"] for code in us_counties if us_counties[code][\"state\"] not in [\"ak\", \"hi\", \"pr\", \"gu\", \"vi\", \"mp\", \"as\"]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To have a better look at what we're manipulating, we can just plot the coordinates using matplotlib." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[