{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from collections import OrderedDict\n",
    "\n",
    "from bokeh.sampledata import us_counties, unemployment\n",
    "from bokeh.plotting import figure, show, output_notebook, ColumnDataSource\n",
    "from bokeh.models import HoverTool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "state = \"tx\"\n",
    "\n",
    "county_xs=[\n",
    "    us_counties.data[code]['lons'] for code in us_counties.data\n",
    "    if us_counties.data[code]['state'] == state\n",
    "]\n",
    "county_ys=[\n",
    "    us_counties.data[code]['lats'] for code in us_counties.data\n",
    "    if us_counties.data[code]['state'] == state\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "colors = [\"#F1EEF6\", \"#D4B9DA\", \"#C994C7\", \"#DF65B0\", \"#DD1C77\", \"#980043\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "county_colors = []\n",
    "county_names = []\n",
    "county_rates = []\n",
    "for county_id in us_counties.data:\n",
    "    if us_counties.data[county_id]['state'] != state:\n",
    "        continue\n",
    "    rate = unemployment.data[county_id]\n",
    "    idx = min(int(rate/2), 5)\n",
    "    county_colors.append(colors[idx])\n",
    "    county_names.append(us_counties.data[county_id]['name'])\n",
    "    county_rates.append(rate)\n",
    "\n",
    "source = ColumnDataSource(\n",
    "    data = dict(\n",
    "        x=county_xs,\n",
    "        y=county_ys,\n",
    "        color=county_colors,\n",
    "        name=county_names,\n",
    "        rate=county_rates,\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "TOOLS=\"pan,wheel_zoom,box_zoom,reset,hover,save\"\n",
    "p = figure(title=\"Texas Unemployment 2009\", tools=TOOLS)\n",
    "p.patches('x', 'y',\n",
    "    fill_color='color', fill_alpha=0.7,\n",
    "    line_color=\"white\", line_width=0.5,\n",
    "    source=source)\n",
    "\n",
    "hover = p.select(dict(type=HoverTool))\n",
    "hover.point_policy = \"follow_mouse\"\n",
    "hover.tooltips = OrderedDict([\n",
    "    (\"Name\", \"@name\"),\n",
    "    (\"Unemployment rate)\", \"@rate%\"),\n",
    "    (\"(Long, Lat)\", \"($x, $y)\"),\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "show(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
