Reputation: 11
I am trying to make Choropleth maps in python using Folium tracking the amount of refugees into each state. My maps are running, but I want to create labels to show the state name and the amount of refugees that appear when I hover/click on each state. I am not sure if this is possible, but I would appreciate any help! Here is the code for my running map Choropleth map.
import pandas as pd
import json
import folium
twentysa=pd.read_excel("State_abrv (9).xlsx")
url = (
"https://raw.githubusercontent.com/python-visualization/folium/master/examples/data"
)
state_geo = f"{url}/us-states.json"
h = folium.Map(location=[48, -102], zoom_start=3)
folium.Choropleth(
geo_data=state_geo,
name="2013 Data",
data=thirteensa,
columns=["State", "Refugee Influx"],
key_on="feature.id",
fill_color="BuPu",
fill_opacity=0.8,
line_opacity=0.3,
legend_name="2013 Refugee Influx",
).add_to(h)
h
Upvotes: 0
Views: 2320
Reputation: 43
You need to use folium.GeoJSON to use the tooltip to enable the hover functionality. I've borrowed the below code from here and it worked for me. This is a starting point to allow the state to display upon hover, but this could be improved upon to show the refugee numbers as well.
# hover functionality
style_function = lambda x: {'fillColor': '#ffffff',
'color':'#000000',
'fillOpacity': 0.1,
'weight': 0.1}
highlight_function = lambda x: {'fillColor': '#000000',
'color':'#000000',
'fillOpacity': 0.50,
'weight': 0.1}
NIL = folium.features.GeoJson(
state_geo,
style_function=style_function,
control=False,
highlight_function=highlight_function,
tooltip=folium.features.GeoJsonTooltip(
fields=['name','id'], # use fields from the json file
aliases=['State: ','ID: '],
style=("background-color: white; color: #333333; font-family: arial; font-size: 12px; padding: 10px;")
)
)
h.add_child(NIL)
h.keep_in_front(NIL)
folium.LayerControl().add_to(h)
Upvotes: 2