Map Generation through Matplotlib and GeoPandas using Python

Haris Mushtaq
2 min readJun 28, 2024

--

This article reflects on the creation of a map through a multi-polygon shapefiles by utilizing Python. Maps are essential for identifying trends and visualizing spatial data in today’s data-driven environment. Python libraries such as Matplotlib and GeoPandas allow you to process geographical data effectively and make customizable according to your requirements.

In this guide, we’ll take you through the steps of map generation from polygon shapefiles. Specifically, you’ll learn how to:

· Set up your Python environment by installing required libraries in order to visualize maps.

· The process of using GeoPandas to read and work with polygon shapefiles from hydrosheds.

· Maps may be made more visually appealing and informative by adding titles, text annotations, and basemaps.

Independent of your role as a GIS specialists, researcher, or data analyst, learning the generation of maps by using Python will enable you to expedite your workflow and produce meaningful inferences quickly. Let’s explore the possibilities of geographical data visualization by utilizing GeoPandas and Matplotlib!

Lets first install the libraries

Read shapefile from Hydrosheds

HydroBASINS represents a series of vectorized polygon layers that depict sub-basin boundaries at a global scale. The goal of this product is to provide a seamless global coverage of consistently sized and hierarchically nested sub-basins at different scales (from tens to millions of square kilometers), supported by a coding scheme that allows for analysis of catchment topology such as up- and downstream connectivity.

In the code below we have read the shapefile file and extracted the kabul and Indus basin shapefile:

we will now plot the shapefiles:

We will now add the text annotation on the map. The text will point at the discharge point of the Indus and Kabul basins at Tarbela reservoir and Nowshera city respectively.

Now we will save the study area map:

plt.savefig('study_area.pdf', bbox_inches='tight')
plt.show()

Here is the output study area map:

Image by Author

In conclusion, utilizing Python with GeoPandas and Matplotlib to automate the creation of maps from multi-polygon shapefiles provides an effective way to visualize geographical data. You have learned a great deal about using Python’s features to make informative and dynamic maps through this lesson.

By using Matplotlib for plotting and GeoPandas for data modification, you can automate the map creation process and alter the graphics to meet your tailored needs. From countries, to mapping regions or other geographical areas that are represented by multi-polygon shapefiles, Python has the ability to create publication-ready maps of regions with high quality.

You are now equipped with essential knowledge on how to do everything from installing required libraries to adding labels, basemaps, scales and legends to maps in order to make them visually appealing and informative. Additionally, the process of generating map for polygons with shape files can ease the process of conducting in depth analysis and exploration.

--

--