Skip to content

Instantly share code, notes, and snippets.

@nro-bot
Created May 3, 2022 02:07
Show Gist options
  • Save nro-bot/5a49fb89ef078103b7e055bdac2447d7 to your computer and use it in GitHub Desktop.
Save nro-bot/5a49fb89ef078103b7e055bdac2447d7 to your computer and use it in GitHub Desktop.
Creating folium map with tooltips (way more difficult than it should be)
# Creating chloropleth with colors scaled to a dataframe is super easy,
# but if you want to add the exact values as a tooltip,
# Have to do it by editing the json that defines the boundaries of the states !
# Quite complicated / annoying if unfamiliar with GeoJSON format.
state_abbr = simplejson.load(open('us_state_capitals.json'))
state_abbr = pd.DataFrame(state_abbr).transpose().reset_index().rename(
columns={'index':'abbr'}
)
reviews_df
'''
city state
12806 Wasilla AK
2581 Anchorage AK
2347 Anchorage AK
'''
my_data = reviews_df['state'].value_counts().reset_index().rename(
columns={'index':'state', 'state':'value'})
my_data
'''
state value
0 CA 200
1 TX 100
'''
my_data = my_data[my_data.state != 'DC'].reset_index()
annotated = pd.merge(state_abbr[['abbr', 'name']], my_data,
left_on='abbr', right_on='state',
how='outer')
annotated.head()
'''
abbr name state value
0 AL Alabama AL 90
1 AK Alaska AK 10
'''
geo_json_data = simplejson.load(open('../data/us-states.json'))
[key for key in geo_json_data]
'''
['type', 'features']
'''
[key for key in geo_json_data['features'][0]]
'''
['type', 'id', 'properties', 'geometry']
'''
geo_json_data['features'][0]['properties']
'''
{'name': 'Alabama'}
'''
# source: https://towardsdatascience.com/using-folium-to-generate-choropleth-map-with-customised-tooltips-12e4cec42af2
# prepare the customised text
tooltip_text = []
for idx, row in annotated.iterrows():
tooltip_text.append(
'State: ' +
row['name'] +'<br> Count: '+
str(row['value']))
tooltip_text
'''
['State: Alabama<br> Count: 90',
'State: Alaska<br> Count: 10',
'''
m = folium.Map([43, -100], zoom_start=4)
chloropleth = folium.Choropleth(
#geo_data=us_states, #built into library
geo_data=geo_json_data,
data=my_data[my_data.state != 'CA'],
columns=['state', 'count'],
key_on='feature.id',
fill_opacity=0.3,
line_weight=2,
legend_name='count',
highlight=True,
).add_to(m)
folium.LayerControl().add_to(m)
chloropleth.geojson.add_child(
folium.features.GeoJsonTooltip(['tooltip1'], labels=False)
)
m
# Save this file: https://github.com/python-visualization/folium/blob/main/examples/data/us-states.json
# Or load with python
import requests
import simplejson
url = 'https://raw.githubusercontent.com/python-visualization/folium/master/examples/data'
us_states = f'{url}/us-states.json'
geo_json_data = simplejson.loads(requests.get(us_states).text)
geo_json_data
'''
{'type': 'FeatureCollection',
'features': [{'type': 'Feature',
'id': 'AL',
'properties': {'name': 'Alabama'},
'geometry': {'type': 'Polygon',
'coordinates': [[[-87.359296, 35.00118],
[-85.606675, 34.984749],
...
[-111.05254, 45.002073],
[-109.080842, 45.002073]]]}}]}
'''
{
"AL": {
"name": "Alabama",
"capital": "Montgomery",
"lat": "32.361538",
"long": "-86.279118"
},
"AK": {
"name": "Alaska",
"capital": "Juneau",
"lat": "58.301935",
"long": "-134.419740"
},
"AZ": {
"name": "Arizona",
"capital": "Phoenix",
"lat": "33.448457",
"long": "-112.073844"
},
"AR": {
"name": "Arkansas",
"capital": "Little Rock",
"lat": "34.736009",
"long": "-92.331122"
},
"CA": {
"name": "California",
"capital": "Sacramento",
"lat": "38.555605",
"long": "-121.468926"
},
"CO": {
"name": "Colorado",
"capital": "Denver",
"lat": "39.7391667",
"long": "-104.984167"
},
"CT": {
"name": "Connecticut",
"capital": "Hartford",
"lat": "41.767",
"long": "-72.677"
},
"DE": {
"name": "Delaware",
"capital": "Dover",
"lat": "39.161921",
"long": "-75.526755"
},
"FL": {
"name": "Florida",
"capital": "Tallahassee",
"lat": "30.4518",
"long": "-84.27277"
},
"GA": {
"name": "Georgia",
"capital": "Atlanta",
"lat": "33.76",
"long": "-84.39"
},
"HI": {
"name": "Hawaii",
"capital": "Honolulu",
"lat": "21.30895",
"long": "-157.826182"
},
"ID": {
"name": "Idaho",
"capital": "Boise",
"lat": "43.613739",
"long": "-116.237651"
},
"IL": {
"name": "Illinois",
"capital": "Springfield",
"lat": "39.783250",
"long": "-89.650373"
},
"IN": {
"name": "Indiana",
"capital": "Indianapolis",
"lat": "39.790942",
"long": "-86.147685"
},
"IA": {
"name": "Iowa",
"capital": "Des Moines",
"lat": "41.590939",
"long": "-93.620866"
},
"KS": {
"name": "Kansas",
"capital": "Topeka",
"lat": "39.04",
"long": "-95.69"
},
"KY": {
"name": "Kentucky",
"capital": "Frankfort",
"lat": "38.197274",
"long": "-84.86311"
},
"LA": {
"name": "Louisiana",
"capital": "Baton Rouge",
"lat": "30.45809",
"long": "-91.140229"
},
"ME": {
"name": "Maine",
"capital": "Augusta",
"lat": "44.323535",
"long": "-69.765261"
},
"MD": {
"name": "Maryland",
"capital": "Annapolis",
"lat": "38.972945",
"long": "-76.501157"
},
"MA": {
"name": "Massachusetts",
"capital": "Boston",
"lat": "42.2352",
"long": "-71.0275"
},
"MI": {
"name": "Michigan",
"capital": "Lansing",
"lat": "42.7335",
"long": "-84.5467"
},
"MN": {
"name": "Minnesota",
"capital": "Saint Paul",
"lat": "44.95",
"long": "-93.094"
},
"MS": {
"name": "Mississippi",
"capital": "Jackson",
"lat": "32.320",
"long": "-90.207"
},
"MO": {
"name": "Missouri",
"capital": "Jefferson City",
"lat": "38.572954",
"long": "-92.189283"
},
"MT": {
"name": "Montana",
"capital": "Helana",
"lat": "46.595805",
"long": "-112.027031"
},
"NE": {
"name": "Nebraska",
"capital": "Lincoln",
"lat": "40.809868",
"long": "-96.675345"
},
"NV": {
"name": "Nevada",
"capital": "Carson City",
"lat": "39.160949",
"long": "-119.753877"
},
"NH": {
"name": "New Hampshire",
"capital": "Concord",
"lat": "43.220093",
"long": "-71.549127"
},
"NJ": {
"name": "New Jersey",
"capital": "Trenton",
"lat": "40.221741",
"long": "-74.756138"
},
"NM": {
"name": "New Mexico",
"capital": "Santa Fe",
"lat": "35.667231",
"long": "-105.964575"
},
"NY": {
"name": "New York",
"capital": "Albany",
"lat": "42.659829",
"long": "-73.781339"
},
"NC": {
"name": "North Carolina",
"capital": "Raleigh",
"lat": "35.771",
"long": "-78.638"
},
"ND": {
"name": "North Dakota",
"capital": "Bismarck",
"lat": "48.813343",
"long": "-100.779004"
},
"OH": {
"name": "Ohio",
"capital": "Columbus",
"lat": "39.962245",
"long": "-83.000647"
},
"OK": {
"name": "Oklahoma",
"capital": "Oklahoma City",
"lat": "35.482309",
"long": "-97.534994"
},
"OR": {
"name": "Oregon",
"capital": "Salem",
"lat": "44.931109",
"long": "-123.029159"
},
"PA": {
"name": "Pennsylvania",
"capital": "Harrisburg",
"lat": "40.269789",
"long": "-76.875613"
},
"RI": {
"name": "Rhode Island",
"capital": "Providence",
"lat": "41.82355",
"long": "-71.422132"
},
"SC": {
"name": "South Carolina",
"capital": "Columbia",
"lat": "34.000",
"long": "-81.035"
},
"SD": {
"name": "South Dakota",
"capital": "Pierre",
"lat": "44.367966",
"long": "-100.336378"
},
"TN": {
"name": "Tennessee",
"capital": "Nashville",
"lat": "36.165",
"long": "-86.784"
},
"TX": {
"name": "Texas",
"capital": "Austin",
"lat": "30.266667",
"long": "-97.75"
},
"UT": {
"name": "Utah",
"capital": "Salt Lake City",
"lat": "40.7547",
"long": "-111.892622"
},
"VT": {
"name": "Vermont",
"capital": "Montpelier",
"lat": "44.26639",
"long": "-72.57194"
},
"VA": {
"name": "Virginia",
"capital": "Richmond",
"lat": "37.54",
"long": "-77.46"
},
"WA": {
"name": "Washington",
"capital": "Olympia",
"lat": "47.042418",
"long": "-122.893077"
},
"WV": {
"name": "West Virginia",
"capital": "Charleston",
"lat": "38.349497",
"long": "-81.633294"
},
"WI": {
"name": "Wisconsin",
"capital": "Madison",
"lat": "43.074722",
"long": "-89.384444"
},
"WY": {
"name": "Wyoming",
"capital": "Cheyenne",
"lat": "41.145548",
"long": "-104.802042"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment