How to create a choropleth map
1. Read, clean, and prepare data to build maps
1.1. Create your first choropleth world map
1.2. Create dictionaries: JSON and GeoJSON
1.3. Read and clean immigration data
2. Problem & Solution
2.1. Fusion of GeoJson and immigration data
2.2. Create your first map with pinpoints
2.3. Using WITS data to build a bar graph
2.4. Insert a bar graph in a pinpoint map
2.5. Obtaining the risk of each country
The following steps will serve as a guideline to create a choropleth using available data on an internet page:
Create a new notebook using the Google Colab environment as described in section 1.2: Designing the solution.
To build a map, let's create a code that uses a folium library to create a map object and display it:
import folium
map = folium.Map(location=[30,10], zoom_start=3, tiles = 'cartodbpositron')
map
This command produces the following figure:
First, create a variable political_countries_url that will store data from a site:
http://geojson.xyz/naturalearth-3.3.0/ne_50m_admin_0_countries.geojson
Then employ folium package to add a political layer to the original map:
import folium
political_countries_url = (
"http://geojson.xyz/naturalearth-3.3.0/ne_50m_admin_0_countries.geojson"
)
# Creating a political layer with the country's borders
political_layer = folium.GeoJson(political_countries_url)
# Adding the political layer to the map
political_layer.add_to(map)
map
This command produces the following figure:
It is also possible to insert a control at the right top of the map using the commands: FeatureGroup and LayerControl(). This control enables to turn on/off the political layer in the map:
from folium import FeatureGroup, LayerControl
# Creating a new map
map = folium.Map(location=[30,10], zoom_start=3, tiles = 'cartodbpositron')
# Creating a feature group...
feature_group1 = FeatureGroup(name='Political')
# ... with the political layer with the countries borders
feature_group1.add_child(political_layer)
# Adding the feature group to the map
map.add_child(feature_group1)
# Adding a on/off control in the upper right corner in the map with the political layer
map.add_child(LayerControl())
map
This command produces the following figure:
The Python code with all the steps is summarized in this Google Colab (click on the link):
https://colab.research.google.com/drive/1BGUOYlSMjsupiz3_lhm9PD1LwwSKSUhg?usp=sharing