Creating maps with embedded charts in Folium
data=gpd.read_file('can_geo_merged.geojson')
data
def style_function(x):
return {'fillColor': x['geometry']['color'],
'color': 'lightblue',
'fill': True,
'stroke': True,
'fillOpacity': .7,
'weight': 2}
def chart_func(df):
df=s[['CMANAME','Instability', 'Deprivation',
'Dependency', 'Ethniccon']].to_frame().T
df=df.melt('CMANAME', var_name='domains', value_name='marginalization score')
chart=alt.Chart(df).mark_bar().encode(
y='domains',
x=alt.X('marginalization score', scale=alt.Scale(domain=[0, 1]))
)
return chart.to_dict()
map_legend_colors = cm.LinearColormap(['#fed16f','#ca0b22'], vmin=0, vmax=2000, caption='Population size')
m = folium.Map(location=[43.0896, -79.0849], zoom_start=10, tiles="Stamen Toner")
for _, s in data.iterrows():
chart=chart_func(s)
coords=list(s['geometry'].exterior.coords)
color=map_legend_colors(s['Pop2016'])
geo_data={"type": "Polygon", "coordinates": [coords], 'color': color}
geo_obj = folium.GeoJson(geo_data, style_function = style_function)
geo_obj.add_child(folium.Popup(max_width=450).add_child(folium.VegaLite(chart)))
geo_obj.add_to(m)
map_legend_colors.add_to(m)
m