Mapping by Geopandas in Colab
One of the most important tools for data visualisation is mapping tools, such as GIS. But advanced GIS software can be expensive, and even if some of them are free to download, but it may not be user-friendly and hard to program. This article shows a simple program by using geopandas in Co-lab to draw the following world map shaded by GDP per capita with a scale bar. (Figure 1).
To make it very simple, I simply do the following three tasks:
- read a shape file of countries with population and gdp data and a vector file of capitals from geopandas datasets;
- plot a world map shaded by GDP per capita with a scale bar; and
- plot the points of the capitals on the world map or plot a map of a particular country.
If you do not know how to use Colab, please refer to my previous article at Yiu (2020). For more information of geopandas, please refer to Geopandas (2021) or Colab (2019).
This simple example has only 8 routines, see whether they work or not. All lines start with a # sign are remarks only:
1. install geopandas and relevant libraries
#install geopython libraries
!apt install gdal-bin python-gdal python3-gdal
#install python3-rtree - Geopandas requirement
!apt install python3-rtree
#install geopandas
!pip install git+git://github.com/geopandas/geopandas.git
#install descartes - Geopandas requirement
!pip install descartes
2. import tools and Linear Regression Algorithm
#import tools: NumPy for Advanced linear algebra, Matplotlib for Visualization and data plotting, Pandas for Data manipulation and analysis, Geopandas for programming geospatial data in python, matplotlib.pyplot for plotting map
import pandas as pd
import numpy as np
import geopandas as gpd
from shapely.geometry import Point
import matplotlib
import matplotlib.pyplot as plt
3. read the shape file of countries from geopandas’ datasets ‘naturalearth_lowres’. It can be read directly by gpd.datasets.get_path(file)
#Read the world data and show the header
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.head()
4. Similarly, the vector file of capitals can be read from geopandas’ datasets ‘naturalearth_cities’. It can be read directly by gpd.datasets.get_path(file)
#Read the capitals data and show the header
capitals = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
capitals.head()
The outputs look like the following tables:
5. Define and plot the world map by calculating GDP per capita
#plot a Map of GDP per capita without using pyplot
#define the map by excluding negative or zero population countries and Antarctica
world = world[(world.pop_est>0) & (world.name!="Antarctica")]
#calculate GDP per capita by dividing GDP by population size
world['gdp_per_cap'] = world.gdp_md_est / world.pop_est
world.plot(column='gdp_per_cap')
The output looks like the following:
6. plot the map with capital markers and with a chosen shading color and a scale bar
Very often, we want to choose different color, provide a legend, a title and a scale bar, etc. The following codes use pyplot to achieve the above.
#Use pyplot (plt) to plot a world map with capital markers
#shaded by gdp per capita with a scale barig, ax = plt.subplots(figsize=(12,6))
world.plot(column='gdp_per_cap', cmap='Blues', ax=ax, legend=True, legend_kwds={'label': "GDP per capita", 'orientation': "horizontal"})
capitals.plot(markersize=0.5, color='red', ax=ax)
plt.show()
I choose light blue shading color, with red markers for capitals and a horizontal scale bar with a title label ‘GDP per capita’. Here is the output:
8. plot a country map
#Use pyplot (plt) to plot a country map such as New Zealand
fig, ax_nz = plt.subplots(figsize=(8,6)) countries = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")) countries[countries["name"] == "New Zealand"].plot(cmap='Blues_r', ax=ax_nz) plt.show()
I have uploaded the codes of mapping by geopandas at the following Github: https://github.com/Chung-collab/great/blob/master/Map_by_geopandas.ipynb for knowledge co-creation.
I also produce a Youtube at TBA (Yiu, 2021) to explain in more details.
References
Geopandas (2021) GeoPandas 0.9.0, https://geopandas.org/index.html
Colab (2019) Part 1 — Introduction to Geographic Data Science.ipynb, Colab, May 20. https://colab.research.google.com/github/shakasom/GDS/blob/master/Part1%20-%20Introduction.ipynb#scrollTo=Ty8djcD4VZMI
Yiu, C.Y. (2020) Learning Machine Learning — How to Code without Learning Coding, Medium, Feb 6. https://ecyy.medium.com/learning-machine-learning-how-to-code-without-learning-coding-9fc4291902b
Yiu, C.Y. (2021) Mapping by Geopandas, Youtube, June 12. https://youtu.be/slJCoo3FmqE