How to plot world population density using GeoPandas

The goal of GeoPandas is to make spatial data processing easier in Python. It provides high-level functions such as the calculation of area or boundary, and basic, choropleth, layered, or interactive plots for multiple geometries and shapes. GeoPandas can read multiple data formats such as json or shp files. It reads spatial data in the form of Geoseries or Geodataframes representing complex Polygons, linestrings, and points to plot geographical areas, paths, or locations.

Geopandas also enables the projection of geographical data in different coordinate systems (CRS), which is an integral part of spatial data processing.

Example

The following is an example of a sample boundary plot created by boundary.plot() method of GeoPandas.

Boundary map of the world
Boundary map of the world

Creating a population density map

To calculate the world population density map, we will use the dataset available on this link. This dataset contains the population and geometry information of all the countries of the world along with some other attributes including but not limited to the country name, code, and region. It is pertinent to note that polygons can consist of hundreds of points and multiple polygons. A snapshot of geometry column is shown below.

Geometry attribute of GeoDataframe
Geometry attribute of GeoDataframe

Since the Dataframe already contains the population attribute, the area is to be calculated to find the population density of all the countries. To calculate the accurate area of countries, the projection of geometry is to be converted into ESPG:6933 which is an equal area projection to preserve the area of the region on the earth as well as on the flat map.
The following code calculates and plots population density using GeoPandas.

# Import relevant libraries
import geopandas as gpd
import matplotlib.pyplot as plt
# Read and process dataset
world_pop = gpd.read_file('https://raw.githubusercontent.com/MinnPost/simple-map-d3/master/example-data/world-population.geo.json')
world_pop['POP2005']=world_pop['POP2005'].astype(float)
world_pop['area']=world_pop.to_crs(6933).area.astype(float)*0.000001
world_pop['density'] = (world_pop['POP2005'].div(world_pop['area']))
world_pop.head()
# Create population density map
plt.title('World Population Density Map')
world_pop.plot(cmap='Blues',linewidth=0.2, scheme='quantiles',
edgecolor='gray',column='density',
legend=True,figsize=(10, 10),
legend_kwds={"loc": "center left", "bbox_to_anchor": (1, 0.5)},
)

Code explanation

Let’s understand the code above:

  • Lines 1–3: Import GeoPandas, matplotlib.

  • Lines 6–7: Read the data using read_file() method of GeoPandas and finally convert the pop2005 column having world population in the year 2005 to float data type.

  • Line 8: Use to_crs() and area() methods of GeoPandas to calculate areas of countries after projecting the CRS. It sldo converts the area from m2m^{2} to km2km^{2}.

  • Lines 9–10: Calculate density by dividing the population by area, it also shows the first few rows of the Dataframe.

  • Line 13: Add title to the graph using plt.title().

  • Lines 14–17: Create the density plot using plot() method of GeoPandas. It uses the following arguments:

    • cmap represents a color map.
    • linewidth sets the width of boundaries between countries.
    • scheme divides the density attribute into different intervals.
    • legend is set to True to include the legend in the figure.
    • figsize() is needed to define the size of the figure.
    • legend_kwds is used for defining the location and size of the legend.

Note: You can practice the above code in the code playground below. Press the run button and wait for the output tab to show the Jupyter Notebook. Alternatively, you can click the link beside the run button to open the respective Jupyter Notebook in a new tab.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved