Using Folium to Generate Choropleth Map with Customised Tooltips (Python) | by Carrie Lo | Towards Data Science | Towards Data Science

Folium is a very good package in visualising data on interactive maps. In this demonstration, Hong Kong voter statistics and population data are used. Data were pre-processed and stored in Consolidated Data.xlsx. Feel free to play around with the dataset =)
You can directly clone the files from my Github repo.
Folium
folium builds on the data wrangling strengths of the Python ecosystem and the mapping strengths of the leaflet.js library. Manipulate your data in Python, then visualize it in on a Leaflet map via folium.
Generate a Simple Map with Different TilesPrepare Customised Data Label and Display on Map when Hovering
Generate a Simple Map with Different Tiles
Prepare Customised Data Label and Display on Map when Hovering
Consolidated Data.xlsx
Task 1: Generate a Simple Map with Different Tiles
hk_geo = r’hkg_adm1.geojson’voter_proportion = data[['Eng_name','Proportion']]map1 = folium.Map([22.38, 114.15], tiles='cartodbpositron', zoom_start=10)tiles = ['stamenwatercolor', 'cartodbpositron', 'openstreetmap', 'stamenterrain']for tile in tiles:    folium.TileLayer(tile).add_to(map1)    choropleth = folium.Choropleth(    geo_data = hk_geo,    name = 'choropleth',    data = voter_proportion,    columns = ['Eng_name', 'Proportion'],    key_on = 'feature.properties.name_1',    fill_color = 'YlGn',    fill_opacity = 0.7,    line_opacity = 0.2,    legend_name = '2019年選民登記比例', # Voter Proportion (%) in 2019    highlight = True).add_to(map1)folium.LayerControl().add_to(map1)# Display Region Labelchoropleth.geojson.add_child(    folium.features.GeoJsonTooltip(['name_1'], labels=False))map1
Step-by-step Explanation:
hk_geo = r’hkg_adm1.geojson’
To plot data on a map, a map json file is needed to locate the position. The json file used in this demonstration is downloaded from this site.
This defines the initial setting of the base map.
tiles = ['stamenwatercolor', 'cartodbpositron', 'openstreetmap', 'stamenterrain']for tile in tiles:    folium.TileLayer(tile).add_to(map1)
Folium supports displaying different tiles in the same map, some common map tiles are shown below. You can find the tile type in the caption of each picture.
folium.LayerControl().add_to(map1)
This code adds the layer selection button (shown on the top right hand corner) to the map, if this code is missed, the map will be shown with the last added tile and the tile style cannot be changed.
choropleth = folium.Choropleth(    geo_data = hk_geo,    name = 'choropleth',    data = voter_proportion,    columns = ['Eng_name', 'Proportion'],    key_on = 'feature.properties.name_1',    fill_color = 'YlGn',    fill_opacity = 0.7,    line_opacity = 0.2,    legend_name = '2019年選民登記比例', # Voter Proportion (%) in 2019    highlight = True).add_to(map1)
folium.Choropleth adds the colored layer to the map based on the data, ‘Proportion’. ‘Eng_name’ is the key of the data to map with the location of the geojson file. Please ensure the spelling of the location name in the json file is the same as the key in the data.
Task 2: Prepare Customised Data Label and Display on Map when Hovering
To add customised data label to a map, folium.features.GeoJsonTooltip is used. By using this code, the labelled values are appended to geojson file.
First, you need to study the structure of the json file and add the tooltip data to the ‘properties’. The above script also allows you to find out the map key to be used in the ‘key_on’ of folium.Choropleth.
key_on='feature.properties.name_1',
In the tooltip, I want to show the district name in Chinese and the data value. After running the code, you will see the tooltip data appended.
# prepare the customised texttooltip_text = []for idx in range(len(data2)): tooltip_text.append(data2[‘Chi_name’][idx]+’ ‘+  str(int(round(data2[‘18–35 (Proportion in 2019)’][idx]*100)))+’%’)tooltip_text# Append a tooltip column with customised textfor idx in range(len(tooltip_text)):    map_data['features'][idx]['properties']['tooltip1'] = tooltip_text[idx]
Expected output:
hk_geo = map_datavoter_proportion = data2[['Eng_name','18-35 (Proportion in 2019)']]map2 = folium.Map([22.38, 114.15], tiles='cartodbpositron', zoom_start=10)choropleth = folium.Choropleth(    geo_data=hk_geo,    name='選民比例',    data=voter_proportion,    columns=['Eng_name','18-35 (Proportion in 2019)'],    key_on='feature.properties.name_1',    fill_color='YlOrBr', # ‘BuGn’, ‘BuPu’, ‘GnBu’, ‘OrRd’, ‘PuBu’, ‘PuBuGn’, ‘PuRd’, ‘RdPu’, ‘YlGn’, ‘YlGnBu’, ‘YlOrBr’, and ‘YlOrRd’.    fill_opacity=0.7,    line_opacity=0.2,    legend_name='2019年18-35歲選民 vs 18-35歲人口比例',    highlight=True).add_to(map2)folium.LayerControl().add_to(map2)choropleth.geojson.add_child(    folium.features.GeoJsonTooltip(['tooltip1'], labels=False))map2.save('map2.html')IFrame('map2.html', width=700, height=450)
The plotting script is similar to the one shown in task 1. In the folium.features.GeoJsonTooltip, please choose the variable name that you store the tooltip data. In my example, it is ‘tooltip1’. labels = False hides the variable name (i.e. ‘tooltip1’) from showing together with data label.
There are also some alternatives to add data label to the plot, but I find this approach more convenient. Feel free to comment if you know other ways to add labels. Hope you enjoy my article =)
To know more about using Folium, please go to official Github repository.
If you find my article useful, please endorse my skills on my linkedIn page to encourage me to write more articles.
Originally published on cydalytics.blogspot.com