Image generated using gencraft

Getting Started with Geospatial Data Visualization with GeoPandas

Manojkumar Patil
Artificial Corner
Published in
5 min readJun 10, 2023

--

In today’s data-driven world, visualizing information on a map can provide valuable insights and help communicate complex patterns effectively. GeoPandas, a Python library built on top of pandas and shapely, empowers users to create stunning maps by merging geospatial data with various variables. In this article, we will explore the capabilities of GeoPandas using the example of plotting variables on a map of Karnataka, India. By the end, you’ll be equipped with the knowledge to create your own impressive maps with rich data overlays.

About GeoPandas

GeoPandas : GeoPandas is a powerful library that extends the capabilities of pandas by adding geospatial data types and operations. It allows us to work with spatial data, such as shapefiles, and perform spatial operations like merging, joining, and plotting.

Let’s Begin!!!

  1. Setting up the environment Before we begin, ensure that you have GeoPandas and its dependencies installed in your Python environment. You can install GeoPandas using pip or conda, and its dependencies, including pandas and matplotlib, will be automatically installed.
pip install geopandas

2. Obtaining the Shapefile: In the demonstration i’m using Karnataka State shapefile obtained from KGIS website.

Wondering what are shapefile? Shapefiles contain both geometric and attribute data for spatial features. You can obtain shapefiles from various sources, such as government repositories or open data portals. Once you have the Karnataka shapefile, place it in your working directory.

3. Loading and Exploring the Shapefile: Using GeoPandas, we can load the Karnataka shapefile into a GeoDataFrame, which is essentially a pandas DataFrame with a geometry column. The geometry column stores the spatial information of each feature in the shapefile.

#Import GeoPandas library 
import geopandas as gpd

# Load the shapefile
gdf_districts = gpd.read_file('District.shp')
This is how the dataframe looks

4. Plotting the Map: With GeoPandas, creating a basic map is as simple as calling the plot() function on the GeoDataFrame. This will generate a visual representation of Karnataka's boundaries.

#plot the map
gdf_districts.plot()

5. Merging Data with the Map: To add data variables to our map, we need to merge the GeoDataFrame with another DataFrame containing the variables we want to visualize. This merging process is usually done using a common identifier, such as a unique geographic code or name.

#Data to be visualised in Map
import pandas as pd
df = pd.read_excel("dist_population.xlsx")
Population data

We can see that we need to two dataframes gdf_districts and df with a common column i.e. District name , but in both dataframe(s) the district name is named differently so we’ll pass the left_on=’KGISDist_1' and right_on=’District Name’ argument inside merge() function and perform a left join.

# Merge shapefile with varibale related to Districts
#KGISDist_1 is our Distrcit Name in gdf_districts dataframe
#District is our Distrcit Name in df dataframe
gdf_merged = gdf_districts.merge(df, left_on='KGISDist_1', right_on='District Name', how='left')

After merging we can see two new columns apperead at the end District Name and Population

This is how merged dataframe looks like

6. Plotting Variable Data on the Map: Once the data is merged, we can leverage the power of GeoPandas to plot the variables on the map. By choosing suitable color palettes, legends, and labels, we can enhance the visual representation and convey meaningful information effectively.

import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np

# Define color scale
cmap = plt.cm.get_cmap('YlOrRd') # Red to green colormap (reversed)
cmap.set_bad('white') # Set NaN values to white
normalize = colors.Normalize(vmin=gdf_merged['Population'].min(), vmax=gdf_merged['Population'].max())

# Plot the map
fig, ax = plt.subplots(figsize=(10, 10))
gdf_districts.plot(ax=ax, facecolor='none', edgecolor='black', linewidth=0.8) # Plot the district outlines

# Fill districts with color based on population values
infes_values = gdf_merged['Population'].fillna(np.nanmin(gdf_merged['Population']) - 1) # Replace NaN values with a value lower than min
gdf_districts.plot(ax=ax, column=infes_values, cmap=cmap, linewidth=0, legend=False)

# Add district labels
for x, y, label in zip(gdf_merged.geometry.centroid.x, gdf_merged.geometry.centroid.y, gdf_merged['KGISDist_1']):
ax.text(x, y, label, fontsize=8, ha='center', va='center')

# Set plot title and axis labels
ax.set_title('Population in Karnataka Districts')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')

# Create and add colorbar
sm = plt.cm.ScalarMappable(cmap=cmap, norm=normalize)
sm.set_array([])
cbar = fig.colorbar(sm)
cbar.set_label('Population')

# Show the plot
plt.show()
Beautiful Map Made by GeoPandas

Ever wondered why Bengaluru, with its massive population, seems to disappear on the map? Well, turns out it’s playing hide and seek with us! In the mysterious realm of data, in gdf_districts Bengaluru is split into Bengaluru (Urban) and Bengaluru (Rural). But when it came time to merge the districts, the merge function got a bit confused and couldn’t find a perfect match in the other dataframe. Talk about a glitch in the matrix! So, folks, remember this cautionary tale of merging mishaps: always handle your data with care, or you might end up misplacing a bustling metropolis on the map!

7. Customizing the Map: GeoPandas provides various customization options to make your map visually appealing. You can adjust colors, apply thematic styling, add labels, and include other elements to improve the overall aesthetics and clarity of the map.

GeoPandas is a versatile library that enables users to create stunning maps by merging geospatial data with various variables. In this article, we explored the process of using GeoPandas to plot variables on a map of Karnataka, India. By following the steps outlined here, you can unleash the power of geospatial data visualization and create your own captivating maps with ease. So go ahead, explore the possibilities, and let your maps tell compelling stories through data.

Remember, the potential of GeoPandas extends far beyond this example. It can be used to analyze and visualize spatial data from different domains, enabling you to uncover hidden patterns, understand relationships, and make informed decisions based on location-based insights.

--

--

Manojkumar Patil
Artificial Corner

Writer for Artifical Corner | Aspiring data analyst | Excel, R, Python, SQL | PhD scholar