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
Let's extract data from WITS. To do so, first install WITS package.
!pip install world_trade_data --upgrade
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ Collecting world_trade_data Downloading world_trade_data-0.1.1.tar.gz (11 kB) Preparing metadata (setup.py) ... Successfully built world_trade_data Installing collected packages: xmltodict, world_trade_data Successfully installed world_trade_data-0.1.1 xmltodict-0.13.0
Now it is possible to read WITS data. The next code obtains the values of Zambia (ZMB) exportation to Rwanda (RWA).
import pandas as pd
import world_trade_data as wits
zmb_exports_2010 = wits.get_indicator('XPRT-TRD-VL', partner='rwa', reporter='zmb', year='2010')
zmb_exports_2010 = zmb_exports_2010.reset_index()
zmb_exports_2010
This will result in:
The next code filter products which are lower than a threshold value in column 'Value'.
filter = zmb_exports_2010['Value'] > 10
zmb_exports_2010_filtered = zmb_exports_2010[filter]
zmb_exports_2010_filtered
The next code will generated the bar graph related with the values in the filtered data table.
fig = plt.figure()
# gca stands for 'get current axis'
ax = plt.gca()
zmb_exports_2010_filtered[['ProductCode', 'Value']].plot.barh(x='ProductCode', y='Value', rot=0, ax = ax)
ax.set_yticklabels(zmb_exports_2010_filtered['ProductCode'])
plt.show()
To enable this bar graph to be inserted into a map, it is necessary to convert it to a html file. To do this, it is necessary to employ the command fig_to_html from the package mpld3. But, first, let's install it.
!pip install mpld3
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ Collecting mpld3 Downloading mpld3-0.5.9-py3-none-any.whl (201 kB) ... Installing collected packages: mpld3 Successfully installed mpld3-0.5.9
import matplotlib.pyplot as plt, mpld3
html_graph = mpld3.fig_to_html(fig)
html_graph
import folium
from ipywidgets import HTML
page='<html>'+html_graph+'</html>'
iframe = folium.IFrame(page,
width=300,
height=300)
popup = folium.Popup(iframe)
display(HTML(page))
The Python code with all the steps is summarized in this Google Colab (click on the link):
https://colab.research.google.com/drive/1w0d5xSKCT9XFB5ADHsurutDSyRK78p-E?usp=sharing