I'm creating a figure in Google Colaboratory with folium. But when I try to output the figure using display()
the runtime disconnects even though the html file of the figure, which I save using map.save(filename)
, opens outside Colab fine when accessing it from my files.
This error also only occurs when I add a 2nd layer to my folium plot.
This is my code if it helps:
def MSOA_Overlay(df, style_function, highlight_function, name):
"""
Makes plot of a clustering.
"""
Plot = folium.features.GeoJson(
df,
name=name,
style_function=style_function,
highlight_function=highlight_function,
tooltip=folium.features.GeoJsonTooltip(
fields=['msoa11nm','label','numneigh','gi','pop','pop_den'],
aliases=['MSOA Name','Cluster Label','Number of Neighbors',
'Common Neighbors Proportion','Population',
'Population Density'],
localize = True,
style=("background-color: white; color: #333333; font-family: arial; font-size: 12px; padding: 10px;")
))
return Plot
#creates map
m1 = folium.Map([54.5,-3],zoom_start=6.8,min_zoom=6,tiles='cartodbpositron')
#titles
title_html = '''
<h3 align="center" style="font-size:16px"><b>{}</b></h3>
'''.format('Spatial Adjacency of MSOAs in Catchment Areas at Time %.4g.' %(time))
gi_colormap.caption = "Proportion of Common Neighbors"
den_colormap.caption = "Log Population Density"
#sets style of Choropleth
gi_style_function = lambda x: {"weight":0.4,
"color":'black',
"opacity":1,
"fillColor":gi_colormap(x['properties']['gi']),
'fillOpacity':0.65}
den_style_function = lambda x: {"weight":0.4,
"color":'black',
"opacity":1,
"fillColor":den_colormap(np.log(x['properties']['pop_den'])),
'fillOpacity':0.65}
#sets style of highlight
highlight_function = lambda x: {'fillColor': '#000000',
'color':'#000000',
'fillOpacity': 0.50}
#makes Choropleth map
MSOA_Overlay(MSOA_stab_df,
gi_style_function,
highlight_function,
'Proportion of Common Neighbors').add_to(m1)
#makes Choropleth map
MSOA_Overlay(MSOA_stab_df,
den_style_function,
highlight_function,
'Population Density').add_to(m1)
#makes boundaries plot
Boundaries = folium.GeoJson(
cluster_stab_df,
name = 'Boundaries',
style_function = lambda x: {
'color': 'black',
'weight': 3,
"opacity":1,
'fillOpacity': 0,
'interactive':False
})
Boundaries.add_to(m1)
#adds to plot
m1.get_root().html.add_child(folium.Element(title_html))
gi_colormap.add_to(m1)
den_colormap.add_to(m1)
folium.LayerControl(collapsed=False).add_to(m1)
for i, row in Pro.iterrows():
marker = folium.CircleMarker(location = [row.geometry.y,row.geometry.x],
radius=3,
color='white',
popup=row['NAME']).add_to(m1)
m1.keep_in_front(marker)
m1.save(root + 'Plots/' + str(time) + '/MSOA_Choropleth.html')
display(m1)
I can add data and the code for the colormaps if that helps.