-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_tools.py
More file actions
56 lines (47 loc) · 2.08 KB
/
plot_tools.py
File metadata and controls
56 lines (47 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import vincent
import json
import pandas as pd
import requests
def mergeToMap(df, df_key_col, map_type):
(map_key, map_path) = getMapParams(map_type)
with open(map_path, 'r') as f:
get_id = json.load(f)
feature_name = get_id['objects'].keys()[0]
#A little FIPS code type casting to ensure keys match
new_geoms = []
district_codes = [x['properties'][map_key] for x in get_id['objects'][feature_name]['geometries']]
districts_df = pd.DataFrame({'code': district_codes}, dtype=str)
#Perform an inner join, pad NA's with data from nearest county
df_merged = pd.merge(df, districts_df, left_on=df_key_col, right_on='code', how='inner')
df_merged = df_merged.fillna(method='pad')
return (df_merged, feature_name)
# Helper function that gets the name of the data structure within a file
def getFeatureName(map_path):
with open(map_path, 'r') as f:
get_id = json.load(f)
return get_id['objects'].keys()[0]
def getMapParams(map_type):
if map_type == 'county':
return ('GEOID', '')
elif map_type == 'congress':
return ('GEOID', 'geography/congress/json/tl_2015_us_cd114.topojson')
elif map_type == 'state':
return('GEOID', '')
def plotMap(df, df_key_col, df_data_col, map_type):
(df_merged, feature_name) = mergeToMap(df, df_key_col, map_type)
(map_key, map_path) = getMapParams(map_type)
map_key_str = 'properties.'+map_key
# Set up map
geo_data = [{'name': 'districts',
'url': map_path,
'feature': feature_name}]
vis = vincent.Map(data=df, geo_data=geo_data, scale=1100,
projection='albersUsa', data_bind=df_data_col,
data_key=df_key_col, map_key={'districts': map_key_str}, brew='YlOrBr') # BuGn, YlGnBu, YlOrBr
# 'brew' color schemes: http://colorbrewer2.org/
vis.marks[0].properties.enter.stroke_opacity = vincent.ValueRef(value=0.5)
#Change our domain for an even inteager
vis.scales['color'].domain = [0, 20]
vis.legend(title='%% Unemployment')
#vis.to_json('vega.json')
return vis