Plotting data on the world map with GeoPandas

Kaveesha Gallage
2 min readJan 25, 2023

--

Hello coders!

This short tutorial describes the step-by-step approach for geospatial data visualization using geopandas and matplotlib. To create the data set, tourist arrivals from main tourist source markets in 2021 which is available in Year in review 2021 report is used.

The data set only contains the country and the number of tourists who arrived from a particular country. To add the longitude and latitude Latitude and Longitude for Every Country and State dataset is used.

After completing the dataset, following steps are performed for data visualization.

The first step is to install geopandas and import relevant libraries.

!pip install geopandas

Importing libraries

Here’s a list of the libraries that are required:

  • Matplotlib is used to import the pyplot package which is used for plotting purposes.
  • Pandas is used for reading and manipulating the dataset from the .csv file.
  • GeoPandas is used to create the world map.
import geopandas
import pandas as pd
import matplotlib.pyplot as plt

The dataset has been loaded using pd.read_csv() function. Here dataset is loaded as a dataframe. Using df.head() we can display the first 5 rows of the dataframe.

df = pd.read_csv('DetailsByCountry.csv',index_col=0)
df.head()

Before continuing we need to handle missing values. To identify whether there are missing values in the dataset below code segment is executed. It displays the count of missing values that are available in each column of the dataframe.

df.isnull().sum()

Since only one row is missing latitude and longitude values, we can handle it by dropping the rows which are having missing values.

df = df.dropna()

After dropping the missing data, we can verify it using the following method.

df.info()

Finally, the GeoPandas library can be utilized to import a world map. By executing plot method of the worldmap variable we can create the world map.

To include the tourist source markets data within the map scatter method is executed Finally, we can include the title for the map and labels for the axes. The size of the scatter is directly proportional to the number of tourists who arrived from the particular country.

# Getting world map data from geo pandas
worldmap = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))

# Creating axes and plotting world map
fig, ax = plt.subplots(figsize=(16, 10))
worldmap.plot(color="lightgrey", ax=ax)

# Plotting tourist source markets
x = df['Longitude']
y = df['Latitude']
z = df['Number of tourists']
plt.scatter(x, y,
s=0.025*z,
c=z,
alpha=0.6,
cmap='autumn'
)
plt.colorbar(label='Number of tourists')

# Creating axis limits and title
plt.xlim([-180, 180])
plt.ylim([-90, 90])

plt.title("Tourist arrivals from main countries to Sri Lanka\n Year : 2021")
plt.xlabel("Longitude")
plt.ylabel("Latitude")
plt.show()

This is the result:

Thank you for reading this tutorial and I hope you get some knowledge about how to plot data in a world map using geopandas. If you have any doubts please reach out to me.

Happy practicing…

--

--

Kaveesha Gallage

Final year undergraduate at Faculty of Information Technology, University of Moratuwa.