Geospatial Visualization
Intermediate
Learning Objectives
After completing this recipe, you will be able to:
- Create scatter plot maps using latitude/longitude data
- Visualize geographic data using GeoPandas
0. Setup
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Generate synthetic location data (near Seoul)
np.random.seed(42)
n_samples = 100
df_geo = pd.DataFrame({
'lat': np.random.uniform(37.4, 37.7, n_samples),
'lon': np.random.uniform(126.8, 127.2, n_samples),
'value': np.random.randint(10, 100, n_samples),
'category': np.random.choice(['A', 'B', 'C'], n_samples)
})1. Simple Latitude/Longitude Scatter Plot
Even without a map file, you can use latitude and longitude as X and Y axes to see an approximate distribution.
plt.figure(figsize=(10, 8))
sns.scatterplot(x='lon', y='lat', hue='category', size='value', sizes=(20, 200), data=df_geo, alpha=0.6)
plt.title('Geospatial Distribution (Scatter)')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.grid(True)
plt.show()실행 결과
[Graph Saved: generated_plot_fc032103bc_0.png]

2. Hexbin Map
Hexbin maps are useful for representing density when you have a lot of data.
plt.figure(figsize=(10, 8))
plt.hexbin(df_geo['lon'], df_geo['lat'], gridsize=15, cmap='YlOrRd', mincnt=1)
plt.colorbar(label='Count')
plt.title('Density Map (Hexbin)')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.show()실행 결과
[Graph Saved: generated_plot_778a70b837_0.png]

3. Real Map Visualization with GeoPandas
Using geopandas, we can visualize data on actual map boundaries (World Map).
import geopandas as gpd
import matplotlib.pyplot as plt
# Load built-in world map dataset
# Note: 'naturalearth_lowres' download or local file required
world = gpd.read_file('src_world_map.zip')
# Create sample data (Capitals of some countries)
data = {
'City': ['Seoul', 'Tokyo', 'New York', 'London', 'Sydney', 'Sao Paulo'],
'Lat': [37.5665, 35.6762, 40.7128, 51.5074, -33.8688, -23.5505],
'Lon': [126.9780, 139.6503, -74.0060, -0.1278, 151.2093, -46.6333],
'Value': [100, 85, 95, 90, 80, 70]
}
df_cities = pd.DataFrame(data)
# Convert to GeoDataFrame
ghub_points = gpd.GeoDataFrame(
df_cities,
geometry=gpd.points_from_xy(df_cities.Lon, df_cities.Lat)
)
# Plot
fig, ax = plt.subplots(figsize=(15, 10))
# 1. Base Map
world.plot(ax=ax, color='lightgrey', edgecolor='white')
# 2. Data Points
ghub_points.plot(
ax=ax,
color='red',
marker='o',
markersize=pd.to_numeric(df_cities['Value']) * 2, # Scale size
alpha=0.7,
label='Major Cities'
)
# Labels
for x, y, label in zip(df_cities.Lon, df_cities.Lat, df_cities.City):
ax.text(x+2, y, label, fontsize=10, fontweight='bold', color='black')
plt.title('Global Distribution Map (GeoPandas)', fontsize=16, fontweight='bold')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.legend()
plt.show()
ℹ️
The map above visualizes the distribution of major hubs using GeoPandas.
Last updated on