Mapping by Geopandas in Colab

4 min readJun 12, 2021


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).

Figure 1 A World Map of GDP per capita

To make it very simple, I simply do the following three tasks:

  1. read a shape file of countries with population and gdp data and a vector file of capitals from geopandas datasets;
  2. plot a world map shaded by GDP per capita with a scale bar; and
  3. 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://
#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'))

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'))

The outputs look like the following tables:

Figure 2 The headers of the Countries and Capitals data files.

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) & (!="Antarctica")]
#calculate GDP per capita by dividing GDP by population size
world['gdp_per_cap'] = world.gdp_md_est / world.pop_est

The output looks like the following:

Figure 3 World map shaded by GDP per capita

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 bar
ig, 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)

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:

Figure 1 Repeated

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)

I have uploaded the codes of mapping by geopandas at the following Github: for knowledge co-creation.

I also produce a Youtube at TBA (Yiu, 2021) to explain in more details.


Geopandas (2021) GeoPandas 0.9.0,

Colab (2019) Part 1 — Introduction to Geographic Data Science.ipynb, Colab, May 20.

Yiu, C.Y. (2020) Learning Machine Learning — How to Code without Learning Coding, Medium, Feb 6.

Yiu, C.Y. (2021) Mapping by Geopandas, Youtube, June 12.




ecyY (Edward Yiu) — easy to understand why, easy to study why. Finding the truths scientifically is the theme.