Plotting Geospatial Data using GeoPandas

GeoPandas is a powerful Python library built on top of Pandas that extends its capabilities to include geospatial data support. Geospatial data describes information related to various locations on Earth's surface, making it valuable for map visualization, urban planning, trade analysis, and network planning. In this article, we'll explore how to plot geospatial data using GeoPandas.

What is GeoPandas?

GeoPandas extends Pandas functionality to handle geometric data types and perform spatial operations. It combines the data manipulation capabilities of Pandas with the geospatial functionality of libraries like Shapely and Fiona. This makes it an excellent choice for working with geographic datasets and creating maps.

Installation

First, install the required packages using pip ?

pip install geopandas geodatasets matplotlib

GeoPandas provides built-in datasets including 'naturalearth_cities', 'naturalearth_lowres', and 'nybb'. We'll use the 'nybb' (New York boroughs) dataset for our examples.

Loading and Exploring Geospatial Data

Let's start by loading the NYC boroughs dataset and examining its structure ?

import geopandas as gpd
from geodatasets import get_path

# Load the NYC boroughs dataset
path = get_path('nybb')
boroughs = gpd.read_file(path)

# Display basic information about the dataset
print("Dataset shape:", boroughs.shape)
print("\nColumn names:", boroughs.columns.tolist())
print("\nFirst few rows:")
print(boroughs.head())
Dataset shape: (5, 5)

Column names: ['BoroCode', 'BoroName', 'Shape_Leng', 'Shape_Area', 'geometry']

First few rows:
   BoroCode   BoroName  Shape_Leng     Shape_Area                                           geometry
0         5  Staten Island   330470.010  1.623820e+09  MULTIPOLYGON (((970217.022 145643.332, 970227...
1         4         Queens   896344.047  3.045213e+09  MULTIPOLYGON (((1029606.077 156073.814, 102957...
2         3       Brooklyn   741080.523  1.937479e+09  MULTIPOLYGON (((1021176.479 151374.797, 102100...
3         1      Manhattan   359299.096  6.364715e+08  MULTIPOLYGON (((981219.056 188655.316, 980940....
4         2          Bronx   464392.991  1.186925e+09  MULTIPOLYGON (((1012821.806 229228.265, 101278...

Basic Plotting

The simplest way to visualize geospatial data is using the built-in plot() method ?

import matplotlib.pyplot as plt

# Basic plot
boroughs.plot(figsize=(8, 6))
plt.title('NYC Boroughs - Basic Plot')
plt.show()

Enhanced Plotting with Styling

We can improve the visualization by adding edge colors and customizing the appearance ?

# Plot with edge colors for better distinction
boroughs.plot(edgecolor='black', linewidth=0.5, figsize=(8, 6))
plt.title('NYC Boroughs with Edge Colors')
plt.axis('off')  # Remove axis for cleaner appearance
plt.show()

Color-Coded Plotting

We can color-code regions based on data columns to show different categories or values ?

# Plot colored by borough name
boroughs.plot(column='BoroName', legend=True, figsize=(10, 6), 
              edgecolor='white', linewidth=0.5)
plt.title('NYC Boroughs Colored by Name')
plt.axis('off')
plt.show()

# Plot colored by area
boroughs.plot(column='Shape_Area', legend=True, figsize=(10, 6),
              edgecolor='black', linewidth=0.5, cmap='Blues')
plt.title('NYC Boroughs Colored by Area')
plt.axis('off')
plt.show()

Filtering and Plotting Specific Regions

Often, you'll want to focus on specific areas. Here's how to filter and plot Manhattan ?

# Filter for Manhattan only
manhattan = boroughs[boroughs['BoroName'] == 'Manhattan']

# Plot Manhattan
manhattan.plot(color='lightblue', edgecolor='red', linewidth=2, figsize=(6, 8))
plt.title('Manhattan Borough')
plt.axis('off')
plt.show()

Creating Subplots

You can create multiple plots in a single figure using subplots ?

# Create subplots for Manhattan and Brooklyn
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))

# Filter data
manhattan = boroughs[boroughs['BoroName'] == 'Manhattan']
brooklyn = boroughs[boroughs['BoroName'] == 'Brooklyn']

# Plot on separate axes
manhattan.plot(ax=ax1, color='lightblue', edgecolor='navy', linewidth=1)
ax1.set_title('Manhattan')
ax1.axis('off')

brooklyn.plot(ax=ax2, color='lightgreen', edgecolor='darkgreen', linewidth=1)
ax2.set_title('Brooklyn')
ax2.axis('off')

plt.tight_layout()
plt.show()

Comparison of GeoPandas Plot Methods

Method Purpose Key Parameters
plot() Basic geometric plotting color, edgecolor, linewidth
plot(column='name') Color by data values column, cmap, legend
plot(ax=axis) Plot on specific subplot ax, figsize

Conclusion

GeoPandas makes plotting geospatial data straightforward with its built-in plot() method. You can create basic maps, apply color coding based on data values, filter specific regions, and create multi-panel visualizations. This powerful combination of Pandas data handling and geospatial visualization makes GeoPandas essential for geographic data analysis.

Updated on: 2026-03-27T09:21:42+05:30

607 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements