This article is shared from the Huawei Cloud Community: Python and Geopandas: A Guide to Geographic Data Visualization and Analysis, author: Lemon Flavored Hug.
Geographic data visualization is critical in many fields, whether you are studying geospatial distribution, urban planning, environmental protection, or business decision-making. The Python language is renowned for its powerful data processing and visualization libraries, and Geopandas, as an extension for the Geographic Information System (GIS) domain, provides convenient tools for working with geospatial data. This article will introduce how to use Python and Geopandas for geographic data visualization and provide practical code examples.
1. Preparation
Before starting, make sure you have installed Python and the Geopandas library. You can use pip to install Geopandas:
pip install geopandas
2. Loading Geographic Data
First, we need to load geographic data. Geopandas supports a variety of geographic data formats, including Shapefile, GeoJSON, Geopackage, and more. In this example, we will use map data in Shapefile format.
import geopandas as gpdRead map data in Shapefile format
world = gpd.read_file(gpd.datasets.get_path(‘naturalearth_lowres’))
3. Data Exploration and Processing
After loading the data, we can perform some basic exploration and processing, such as viewing the first few rows of the data, data types, and more.
# View the first few rows of the data print(world.head())View the column names of the data
print(world.columns)
View the geometry type of the data
print(world.geom_type)
4. Geographic Data Visualization
Next, let's use the Matplotlib library to visualize our geographic data.
import matplotlib.pyplot as pltPlot the map
world.plot()
plt.show()
5. Custom Map Styling
You can also customize the map style, such as changing colors, adding labels, and more.
# Custom map styling
world.plot(color='lightblue', edgecolor='black')
plt.title('World Map')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.show()
6. Adding Data
In addition to drawing base maps, we can also add other data to the map to provide more information.
# Add additional data
cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
world.plot()
cities.plot(marker='o', color='red', markersize=5)
plt.show()
7. Spatial Analysis and Querying
Geopandas can be used not only for geographic data visualization, but also for spatial analysis and querying. For example, we can use spatial queries to find other locations near a given point of interest.
from shapely.geometry import PointCreate a Point object representing the latitude and longitude of a location
point = Point(-74.006, 40.7128)
Spatial query: find the city closest to this point
nearest_city = cities[cities.distance(point).idxmin()]
print(“The nearest city is:”, nearest_city[‘name’])
8. Map Overlay and Grouping
In map visualization, it is sometimes necessary to overlay different geographic datasets and display them grouped by certain conditions.
# Group by continent
world_grouped = world.groupby('continent').agg({'geometry': 'union'})
world_grouped.plot()
plt.title('World Map Grouped by Continent')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.show()
9. More Advanced Geographic Data Operations
In addition to the basic operations described above, Geopandas supports more advanced geographic data operations such as spatial buffering, spatial overlay, and geographic topological relationship analysis.
# Spatial buffer example
buffered_area = world.geometry.buffer(5)
buffered_area.plot()
plt.title('Buffered World Map')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.show()
13. Interactive Geographic Data Visualization
In addition to static geographic data visualization, you can also use interactive tools to explore and present geographic data. Bokeh and Folium are two commonly used Python libraries that enable interactive geographic data visualization.
import foliumCreate a map object
m = folium.Map(location=[40.7128, -74.006], zoom_start=10)
Add city markers
for idx, row in cities.iterrows():
folium.Marker([row[‘latitude’], row[‘longitude’]], popup=row[‘name’]).add_to(m)Display the map
m
14. Multi-layer Overlay and Control
In interactive maps, you can add multiple layers and provide control options to allow users to customize the displayed content.
# Create a map object m = folium.Map(location=[40.7128, -74.006], zoom_start=10)Add the world map layer
folium.GeoJson(world).add_to(m)
Add the cities layer
city_layer = folium.FeatureGroup(name=‘Cities’)
for idx, row in cities.iterrows():
folium.Marker([row[‘latitude’], row[‘longitude’]], popup=row[‘name’]).add_to(city_layer)
city_layer.add_to(m)Add layer control
folium.LayerControl().add_to(m)
Display the map
m
15. Data Integration and Visualization Applications
By integrating geographic data visualization with other data, you can implement richer application scenarios. For example, you can combine population data, economic indicators, and other information to conduct more in-depth geographic data analysis and visualization.
# Read population data population_data = pd.read_csv("population.csv")Merge population data with city data by city name
cities_with_population = pd.merge(cities, population_data, how=‘left’, on=‘name’)
Plot cities on the map and adjust marker size based on population
m = folium.Map(location=[40.7128, -74.006], zoom_start=4)
for idx, row in cities_with_population.iterrows():
folium.CircleMarker(location=[row[‘latitude’], row[‘longitude’]], radius=row[‘population’] / 100000,
fill_color=‘blue’, fill_opacity=0.6).add_to(m)
m
16. Geographic Data Analysis and Visualization Case Study
Let's walk through a case study to demonstrate how to use Python and Geopandas for geographic data analysis and visualization. Suppose we have a dataset of GDP and population for countries around the world, and we want to analyze the economic and demographic conditions of each country and visualize the results.
# Read GDP and population data gdp_data = pd.read_csv("gdp_data.csv") population_data = pd.read_csv("population_data.csv")Merge the data into a single DataFrame
world_data = pd.merge(world, gdp_data, how=‘left’, left_on=‘name’, right_on=‘Country Name’)
world_data = pd.merge(world_data, population_data, how=‘left’, left_on=‘name’, right_on=‘Country Name’)Calculate GDP per capita
world_data[‘GDP per capita’] = world_data[‘GDP (current US$)’] / world_data[‘Population’]
Plot the GDP per capita map
fig, ax = plt.subplots(1, 1, figsize=(10, 6))
world_data.plot(column=‘GDP per capita’, cmap=‘OrRd’, linewidth=0.8, ax=ax, edgecolor=‘0.8’, legend=True)
ax.set_title(‘World GDP per Capita’)
plt.show()
17. Analysis Results
With the code above, we get a map of GDP per capita for all countries in the world, from which we can clearly see the differences in economic development levels between different countries. Next, we can further analyze issues such as population density and uneven regional development, and propose corresponding policy recommendations.
# Calculate population density world_data['Population Density'] = world_data['Population'] / world_data.geometry.areaPlot the population density map
fig, ax = plt.subplots(1, 1, figsize=(10, 6))
world_data.plot(column=‘Population Density’, cmap=‘Blues’, linewidth=0.8, ax=ax, edgecolor=‘0.8’, legend=True)
ax.set_title(‘World Population Density’)
plt.show()
18. Conclusion and Outlook
Through the introduction and case study in this article, we have learned how to use Python and Geopandas for geographic data analysis and visualization. Geographic data analysis and visualization helps us more deeply understand the spatial distribution and characteristics of the Earth, providing stronger support for decision-making.
In the future, with the continuous development of data collection and processing technology, geographic data analysis and visualization will play an increasingly important role, providing more useful information and insights for the sustainable development of human society and environmental protection.
Thank you for reading this article, hope it inspires and helps you!
Summary
This article deeply explores how to use Python and Geopandas for geographic data visualization and analysis, and provides a wealth of code examples and a complete case study. Below is a summary of the main points of this article:
-
Preparation: Before starting, you need to make sure you have installed Python and the Geopandas library, which you can install using pip.
-
Loading Geographic Data: Geopandas supports multiple geographic data formats including Shapefile, GeoJSON, Geopackage, and more. You can use the
gpd.read_file()function to load data. -
Data Exploration and Processing: After loading data, you can perform basic exploration and processing tasks such as viewing the first few rows of data, column names, and data types.
-
Geographic Data Visualization: You can use the Matplotlib library to visualize geographic data, and customize maps by adjusting styles, adding labels, and other methods.
-
Spatial Analysis and Querying: Geopandas supports spatial analysis and querying operations such as spatial queries and spatial buffering.
-
Data Saving and Export: You can use Geopandas to save geographic data to files in formats such as Shapefile and GeoJSON.
-
Data Projection and Coordinate Conversion: Geopandas supports data projection and coordinate conversion, allowing you to reproject maps to different projection systems.
-
Interactive Geographic Data Visualization: Libraries such as Bokeh and Folium enable interactive geographic data visualization, enhancing the interactivity of data exploration and presentation.
-
Geographic Data Analysis and Visualization Case Study: Through the case study, we demonstrated how to use Python and Geopandas to analyze the economic and demographic conditions of countries around the world and visualize the results.
-
Conclusion and Outlook: Geographic data analysis and visualization have broad applications in many fields, and with the development of technology, they will continue to provide us with more useful information and insights.
After working through this article, readers can master the basic methods of using Python and Geopandas to process and visualize geographic data, gaining support and guidance for practical applications.
Click to follow to get the latest updates on Huawei Cloud technologies as soon as possible~
This is a discussion topic separated from the original thread at https://juejin.cn/post/7368820207592505353