from jq import jq
import pandas as pd
import urllib
import json
import copy
topology = json.loads(urllib.urlopen('https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json').read())
single_rule = '''
.features[] | select(.geometry.type == "Polygon" ) |
{
country_id: .id,
country_name: .properties.name,
xs: .geometry.coordinates[] | map(.[0]),
ys: .geometry.coordinates[] | map(.[1])
}
'''
single_scrubbed = jq(single_rule).transform(topology, multiple_output=True)
multi_rule = '''
.features[] | select(.geometry.type == "MultiPolygon" ) |
{
country_id: .id,
country_name: .properties.name,
coordinates: .geometry.coordinates[][]
} | {
country_id: .country_id,
country_name: .country_name,
xs: .coordinates | map(.[0]),
ys: .coordinates | map(.[1]),
}
'''
multi_scrubbed = jq(multi_rule).transform(topology, multiple_output=True)
countries = pd.DataFrame(single_scrubbed + multi_scrubbed)
name_scrubbers = {
'United Kingdom': 'Britain',
'United Arab Emirates': 'UAE',
'United States of America': 'United States',
'Greenland': 'Denmark'
}
for idx, row in countries.iterrows():
if row['country_name'] in name_scrubbers.keys():
countries['country_name'][idx] = name_scrubbers[row['country_name']]
print countries.head()
country_id country_name \ 0 AFG Afghanistan 1 ALB Albania 2 ARE UAE 3 ARM Armenia 4 ATF French Southern and Antarctic Lands xs \ 0 [61.210817, 62.230651, 62.984662, 63.193538, 6... 1 [20.590247, 20.463175, 20.605182, 21.02004, 20... 2 [51.579519, 51.757441, 51.794389, 52.577081, 5... 3 [43.582746, 44.97248, 45.179496, 45.560351, 45... 4 [68.935, 69.58, 70.525, 70.56, 70.28, 68.745, ... ys 0 [35.650072, 35.270664, 35.404041, 35.857166, 3... 1 [41.855404, 41.515089, 41.086226, 40.842727, 4... 2 [24.245497, 24.294073, 24.019826, 24.177439, 2... 3 [41.092143, 41.248129, 40.985354, 40.81229, 40... 4 [-48.625, -48.94, -49.065, -49.255, -49.71, -4...
from bokeh.plotting import show, output_notebook
from bokeh.resources import CDN
from bokeh.models import ColumnDataSource, Patches, Range1d, Plot, HoverTool
from bokeh.models.glyphs import Patches
from collections import OrderedDict
def setup_map_plot():
x_start, x_end = (-180, 180)
y_start, y_end = (-85, 85)
aspect_ratio = (x_end - x_start) / (y_end - y_start)
x_range = Range1d(x_start, x_end)
y_range = Range1d(y_start, y_end)
plot_height = 500
return Plot(x_range=x_range, y_range=y_range, title="World Map", plot_width=int(plot_height * aspect_ratio), plot_height=plot_height)
map_box = setup_map_plot()
map_box.add_glyph(ColumnDataSource(countries), Patches(xs='xs', ys='ys'))
output_notebook(resources=CDN)
show(map_box)
prices_2015 = pd.DataFrame.from_csv((urllib.urlopen('https://raw.githubusercontent.com/mistakevin/BigMacBokeh/master/BMfile2000-Jul2015/Jul2015-Table%201.csv')))
prices_2015['country_name'] = ''
for idx, row in prices_2015.iterrows():
prices_2015['country_name'][idx] = idx
prices_2015 = prices_2015.set_index('country_name')
# print prices_2015[['dollar_valuation']].head()
print prices_2015.head()
local_price dollar_ex dollar_price dollar_ppp \ country_name Argentina 28.00 9.14 3.07 5.85 Australia 5.30 1.35 3.92 1.11 Brazil 13.50 3.15 4.28 2.82 Britain 2.89 0.64 4.51 0.60 Canada 5.85 1.29 4.54 1.22 dollar_valuation dollar_adj_valuation euro_adj_valuation \ country_name Argentina -36.01 -4.76 -0.36 Australia -18.12 -22.17 -18.57 Brazil -10.59 35.07 41.32 Britain -5.79 1.33 6.01 Canada -5.30 -2.07 2.45 sterling_adj_valuation yen_adj_valuation yuan_adj_valuation country_name Argentina -6.0 30.9 5.0 Australia -23.2 6.9 -14.2 Brazil 33.3 85.6 49.0 Britain 0.0 39.2 11.7 Canada -3.4 34.6 8.0
colors = [
{
'min_dollar_valuation': float('-inf'),
'max_dollar_valuation': -50,
'color': '#E63F2E'
},
{
'min_dollar_valuation': -50,
'max_dollar_valuation': -25,
'color': '#E38C76'
},
{
'min_dollar_valuation': -25,
'max_dollar_valuation': -10,
'color': '#FFB39C'
},
{
'min_dollar_valuation': -10,
'max_dollar_valuation': 10,
'color': '#E6DA96'
},
{
'min_dollar_valuation': 10,
'max_dollar_valuation': 50,
'color': '#BACFD8'
},
{
'min_dollar_valuation': 50,
'max_dollar_valuation': 100,
'color': '#649AA7'
},
{
'min_dollar_valuation': 100,
'max_dollar_valuation': float('inf'),
'color': '#16526D'
}
]
countries_with_prices = countries.join(prices_2015, how='outer', on='country_name')
countries_with_prices['raw_fill_color'] = '#F2F2F2'
print countries_with_prices[['country_id', 'country_name', 'dollar_valuation', 'raw_fill_color']].head(7)
country_id country_name dollar_valuation \ 0 AFG Afghanistan NaN 1 ALB Albania NaN 2 ARE UAE -26.11 3 ARM Armenia NaN 4 ATF French Southern and Antarctic Lands NaN 5 AUT Austria -22.46 6 BDI Burundi NaN raw_fill_color 0 #F2F2F2 1 #F2F2F2 2 #F2F2F2 3 #F2F2F2 4 #F2F2F2 5 #F2F2F2 6 #F2F2F2
def color_code(color_column, value_column):
countries_with_prices[color_column] = '#F2F2F2'
for idx, row in countries_with_prices.iterrows():
for obj in colors:
if row[value_column] <= obj['max_dollar_valuation'] and row[value_column] >= obj['min_dollar_valuation']:
countries_with_prices[color_column][idx] = obj['color']
color_code('raw_fill_color', 'dollar_valuation')
print countries_with_prices[['country_name', 'dollar_valuation', 'raw_fill_color']].head(7)
country_name dollar_valuation raw_fill_color 0 Afghanistan NaN #F2F2F2 1 Albania NaN #F2F2F2 2 UAE -26.11 #E38C76 3 Armenia NaN #F2F2F2 4 French Southern and Antarctic Lands NaN #F2F2F2 5 Austria -22.46 #FFB39C 6 Burundi NaN #F2F2F2
raw_map = setup_map_plot()
raw_map.add_glyph(ColumnDataSource(countries_with_prices), Patches(xs='xs', ys='ys', fill_color='raw_fill_color'))
output_notebook(resources=CDN)
show(raw_map)
# tooltips = "@country_name"
# map_box.add_tools(HoverTool(tooltips=tooltips))
# show(raw_map)
color_code('adj_fill_color', 'dollar_adj_valuation')
print countries_with_prices[['country_name', 'dollar_adj_valuation', 'adj_fill_color']].head()
country_name dollar_adj_valuation adj_fill_color 0 Afghanistan NaN #F2F2F2 1 Albania NaN #F2F2F2 2 UAE NaN #F2F2F2 3 Armenia NaN #F2F2F2 4 French Southern and Antarctic Lands NaN #F2F2F2
adj_map = setup_map_plot()
adj_map.add_glyph(ColumnDataSource(countries_with_prices), Patches(xs='xs', ys='ys', fill_color='adj_fill_color'))
output_notebook(resources=CDN)
show(adj_map)
from bokeh.models.widgets import Panel, Tabs
raw_panel = Panel(child=raw_map, title="Raw index")
adj_panel = Panel(child=adj_map, title="Adjusted index")
tabs = Tabs(tabs=[ raw_panel, adj_panel ])
show(tabs)