Plot Geographical Data on a Map Using Python Plotly

Geographial Plot

In this tutorial, we will learn how to plot geographical data on a map using Python Plotly. For this demonstration, we’ll plot COVID-19 cases from ourworldindata.org dataset.

Steps to Plot Geographical Data on a Map in Python

Let’s get started.

1. Import the required libraries

Let’s start with importing the necessary libraries. We need to import the following two libraries:

  1. Pandas
  2. Plotly.express
import pandas as pd
import plotly.express as px

Now we can move to the next step, that is downloading the dataset.

2. Download and read the dataset

To download and load the dataset, use the following piece of code.

!wget https://covid.ourworldindata.org/data/owid-covid-data.csv

Output :

Download Dataset
Download Dataset

Read the CSV dataset in Python using the pandas read_csv method:

df = pd.read_csv('owid-covid-data.csv')
df.head()
Dataset
Dataset

3. Plotting the COVID-19 Dataset on a Map

Now we can use Plotly to plot the data from the dataset above.

We are plotting a Choropleth Map. It is a map which uses differences in shading, colouring, or the placing of symbols within predefined areas to indicate the average values of a particular quantity in those areas.

We are going to plot the number of new cases each day. Plotly lets us do that through animations.

Here’s the code to plot the colors and shades on the map:

fig = px.choropleth(df, locations="iso_code",
                    color="new_cases",
                    hover_name="location",
                    animation_frame="date",
                    title = "Covid Cases plotted using Plotly",                 color_continuous_scale=px.colors.sequential.PuRd)


fig["layout"].pop("updatemenus")
fig.show()

Output :

Feb
Feb
Plot Geographical Data - April
April
Plot Geographical Data in Python Plotly - September
September

The output shows how the map looks over three different months of the year. We can see that the virus was spreading rapidly in China in the month of February, in USA in the month of April and in India in the month of September.

You can also hover over any region of the map and view the number of new cases.

Plotly animations make it convenient to visualize time series data.

4. Complete code to Plot Geographical Data using Python Plotly

The complete code for this section in given below :

import pandas as pd
import plotly.express as px

#download dataset
!wget https://covid.ourworldindata.org/data/owid-covid-data.csv

#import dataset
df = pd.read_csv('owid-covid-data.csv')

#plot
fig = px.choropleth(df, locations="iso_code",
                    color="new_cases",
                    hover_name="location",
                    animation_frame="date",
                    title = "Covid Cases plotted using Plotly",                 color_continuous_scale=px.colors.sequential.PuRd)


fig["layout"].pop("updatemenus")
fig.show()

6. Plotting COVID-19 Asia Data

You can also set the scope of the map to Asia. Let’s see how to plot geographical data for the content of Asia.

To set the scope of the plot to Asia, set the parameter ‘scope‘ to ‘asia’.

import pandas as pd
import plotly.express as px

#download dataset
!wget https://covid.ourworldindata.org/data/owid-covid-data.csv

#import dataset
df = pd.read_csv('owid-covid-data.csv')

#select entries with the continent as asia
df = df[df.continent == 'Asia']

#plot
fig = px.choropleth(df, locations="iso_code",
                    color="new_cases",
                    hover_name="location",
                    animation_frame="date",
                    title = "Daily new COVID cases",
                    scope ='asia',  color_continuous_scale=px.colors.sequential.PuRd)

fig["layout"].pop("updatemenus")
fig.show()

Output :

Covid cases in Asia

This video shows new cases of COVID-19 for each day with the help of animation.

Conclusion

This tutorial was on plotting geographical data in Python Plotly. We plotted data from the Covid-19 dataset using Plotly in python. To learn about other types of maps that Plotly lets you create, read their official documentation.