Created
April 21, 2015 07:59
-
-
Save raghothams/96318836c8670a19dd70 to your computer and use it in GitHub Desktop.
choropleth viz with bokeh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from bokeh.plotting import *\n", | |
"from bokeh.models import HoverTool\n", | |
"from collections import OrderedDict\n", | |
"import json" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"with open(\"/home/init/code/indian-states/ka_tn.geojson\") as f:\n", | |
" data = f.readlines()\n", | |
"len(data)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 4, | |
"text": [ | |
"1" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# convert json ot dict\n", | |
"data = \"\".join(data)\n", | |
"data = json.loads(data)\n", | |
"lats = []\n", | |
"lons = []\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"data['features'][0]['properties']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 6, | |
"text": [ | |
"{u'ENGTYPE_2': u'District',\n", | |
" u'ID_0': 105,\n", | |
" u'ID_1': 17,\n", | |
" u'ID_2': 216,\n", | |
" u'ISO': u'IND',\n", | |
" u'NAME_0': u'India',\n", | |
" u'NAME_1': u'Karnataka',\n", | |
" u'NAME_2': u'Bagalkot',\n", | |
" u'NL_NAME_2': u'',\n", | |
" u'TYPE_2': u'District',\n", | |
" u'VARNAME_2': u''}" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# filter only karnataka\n", | |
"features = data['features']\n", | |
"filtered = filter(lambda x: x['properties']['NAME_1'] == 'Karnataka', features)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# get xs, ys (lat, lon) for map\n", | |
"count = 0\n", | |
"for district in filtered:\n", | |
" count += 1\n", | |
" for x in district['geometry']['coordinates'][0]:\n", | |
" lats.append(x[0])\n", | |
" lons.append(x[1])\n", | |
" " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"count" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 10, | |
"text": [ | |
"27" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# not required\n", | |
"lats_new = []\n", | |
"for x in lats:\n", | |
" if type(x) is float:\n", | |
" lats_new.append(x)\n", | |
" \n", | |
"lons_new = []\n", | |
"for x in lons:\n", | |
" if type(x) is float:\n", | |
" lons_new.append(x) " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"output_file(\"choropleth-kka.html\", title=\"choropleth.py example\")\n", | |
"\n", | |
"TOOLS=\"pan,wheel_zoom,box_zoom,reset,hover,save\"\n", | |
"p = figure(title=\"KA TN\", tools=TOOLS, toolbar_location=\"left\",\n", | |
" plot_width=1100, plot_height=700)\n", | |
"\n", | |
"p.patches( [lats_new], [lons_new], fill_color=\"#F1EEF6\", fill_alpha=0.0,\n", | |
" line_color=\"blue\", line_width=2, )\n", | |
"\n", | |
"\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 14, | |
"text": [ | |
"<bokeh.plotting.Figure at 0x7f688ab19950>" | |
] | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"show(p)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 15 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment