from bokeh.io import show, output_notebook
from bokeh.models import (
GeoJSONDataSource,
ColumnDataSource,
HoverTool,
LogColorMapper
)
from bokeh.palettes import Viridis6 as palette
from bokeh.plotting import figure
from bokeh.sampledata.unemployment import data as unemployment
import json
from collections import OrderedDict
output_notebook()
def merge_unemployment(county_dict, unemployment):
countyid = int(county_dict['properties']['STATEFP']), int(county_dict['properties']['COUNTYFP'])
county_dict['properties']['rate'] = unemployment[countyid]
return county_dict
# source: https://www.census.gov/geo/maps-data/data/cbf/cbf_counties.html
# ideally add unemployment rate as an attribute to the input geojson??
# read all counties
with open(r'D:\Data\US_Census\cb_2015_us_county_20m.geojson', 'r') as f:
us_counties_dict = json.loads(f.read(), object_hook=OrderedDict)
# Keep Texas: STATEFP = '48'
us_counties_dict['features'] = [subd for subd in us_counties_dict['features'] if subd['properties']['STATEFP'] == '48']
# merge unemployment rate
us_counties_dict['features'] = [merge_unemployment(subd, unemployment) for subd in us_counties_dict['features']]
geo_source = GeoJSONDataSource(geojson=json.dumps(us_counties_dict))
palette.reverse()
color_mapper = LogColorMapper(palette=palette)
TOOLS = "pan,wheel_zoom,box_zoom,reset,hover,save"
p = figure(
title="Texas Unemployment, 2009", tools=TOOLS,
x_axis_location=None, y_axis_location=None
)
p.grid.grid_line_color = None
p.patches('xs', 'ys', source=geo_source,
fill_color={'field': 'rate', 'transform': color_mapper},
fill_alpha=0.7, line_color="white", line_width=0.5)
hover = p.select_one(HoverTool)
hover.point_policy = "follow_mouse"
hover.tooltips = [
("Name", "@NAME"),
("Area Land)", "@rate%"),
("(Long, Lat)", "($x, $y)"),
]
show(p)