How to Plot Data on a World Map in Python?

Athisha R K
Analytics Vidhya
Published in
4 min readSep 18, 2020
A Digital Display of World Map with some places marked in red dots
Photo by Martin Sanchez on Unsplash

I learned a new thing this week. In Machine Learning class, I clustered weather stations using DBSCAN (maybe topic for another article) and plotted those results on a world map. For this, I used the Basemap package in python.

What is Basemap?

Basemap is a tool to create beautiful maps in python. It is an extension of Matplotlib. Using Basemap, we can plot coastal lines and countries directly.

Prerequisites

  • Basics of Matplotlib and Pandas in Python
  • Experience with Jupyter Notebooks or Google Colab Notebooks (or any other equivalent)

Lets do this!

I’m using Google Colab for this tutorial. First, we need to install the required packages to use the Basemap in python.

Install The Required Packages

Here, I’m importing the packages which we will be using later.

Import the Packages

We can use any dataset which has Latitude and Longitude as features. Here, I’m using the cities.csv dataset provided by my instructor (You can find a similar dataset on kaggle). Create a Pandas data frame using that dataset file.

Create a Data Frame using dataset.csv

The dataset which I have used has 245 entries and 8 features (including latitude and longitude).

Data Frame Shape and Head

We need only the latitude and longitude for plotting the map. So the following piece of code creates a data frame having only those 2 features.

Extract the required features from the data frame

Set the upper bound and lower bound of latitude and longitude

Upper and Lower Bound of Latitude and Longitude

But how to get the values to set the upper bound and lower bound? I used OpenStreetMap export feature to find those values for South America region.

OpenStreetMap Export Feature

Extract only those data points that lie in the given region. There are 47 data points to plot in the South America region.

Retrieve data points in the specified area

Create the Basemap with necessary parameters like upper bound and lower bound of longitude and latitude.

Create Basemap

Draw coastal lines and countries. xs and ys are relative coordinates on the map. Iterate through the dataset and plot each point on the map. The last line in the below code snippet downloads the image to the computer (or to /content in Google Colab).

Draw the map

Output of the above code snippet

The Final Result!! :)

The below code snippet can be used to display an image in python notebook. Here, it displays the map that was downloaded before.

Code to display an image in python

Output

ExampleMap.png

The above image is an example of how powerful the Basemap package is. Using the Basemap package we could create many more such maps with ease.

Photo by Kelly Sikkema on Unsplash

Hope this helps. Let me know your views/suggestions/questions in the comments section below. :)

GitHub Link: https://github.com/athisha-rk/mediumArticlesRelatedFiles/blob/master/Basemap_Example.ipynb

--

--

Athisha R K
Analytics Vidhya

Infrastructure Engineer @ Lowe’s India | Python Programmer