This article is shared from the Huawei Cloud Community " Python and Geopandas: Geographic Data Visualization and Analysis Guide " by Lemony Hug.
Geodata visualization is crucial in many fields, whether studying geospatial distribution, urban planning, environmental protection, or business decision-making. The Python language is known for its powerful data processing and visualization libraries, and Geopandas, as an extension of its geographic information system (GIS) domain, provides convenient tools for processing 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 Python and the Geopandas library installed. Geopandas can be installed using pip:
pip install geopandas
2. Load geographic data
First, we need to load the geographic data. Geopandas supports multiple geographic data formats, including Shapefile, GeoJSON, Geopackage, etc. In this example, we will use map data in a Shapefile format.
import geopandas as gpd # Read 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 do some basic exploration and processing, such as viewing the first few rows of the data, data type, etc.
# View the first few rows of data print(world.head()) # View the column names of the data print(world.columns) # Check the geometry type of the data print(world.geom_type)
4. Geographic data visualization
Next, let's visualize the geographic data using the Matplotlib library.
import matplotlib.pyplot as plt # draw map world.plot() plt.show()
5. Customize map style
You can also customize the map's style, such as changing colors, adding labels, and more.
# Custom map style world.plot(color='lightblue', edgecolor='black') plt.title('World Map') plt.xlabel('Longitude') plt.ylabel('Latitude') plt.show()
6. Add data
In addition to drawing maps, we can also add other data to the map to provide more information.
#Add other 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 query
Geopandas can be used not only for visualization of geographical data, but also for spatial analysis and query. For example, we can use spatial queries to find other places near a location.
from shapely.geometry import Point #Create a point object to represent the latitude and longitude of a certain location point = Point(-74.006, 40.7128) # Spatial query to find the city closest to the 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 geographical data and display them in groups according to 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 complex geographic data operations
In addition to the above basic operations, Geopandas also supports more complex geographical data operations, such as spatial buffers, spatial overlays, geographical topological relationship analysis, etc.
# Space 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, interactive tools can also be used to explore and display geographic data. Bokeh and Folium are two commonly used Python libraries that enable interactive geographic data visualization.
import leaf #Create a map object m = folium.Map(location=[40.7128, -74.006], zoom_start=10) #Add city tag for idx, row in cities.iterrows(): folium.Marker([row['latitude'], row['longitude']], popup=row['name']).add_to(m) # show map m
14. Multi-layer overlay and control
In an interactive map, you can add multiple layers and provide controls to customize the display.
#Create a map object m = folium.Map(location=[40.7128, -74.006], zoom_start=10) #Add world map layer folium.GeoJson(world).add_to(m) #Add city 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) # show map m
15. Data integration and visualization applications
By integrating geographic data visualization with other data, richer application scenarios can be achieved. For example, combine population data, economic indicators and other information to conduct more in-depth geographical data analysis and visual display.
# Read population data population_data = pd.read_csv("population.csv") # Merge population data with city data based on city name cities_with_population = pd.merge(cities, population_data, how='left', on='name') # Draw 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 cases
Let us use a case to demonstrate how to use Python and Geopandas for geographic data analysis and visualization. Suppose we have a set of data about the GDP and population of various countries in the world. We want to analyze the economic and demographic situation 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") # Combine data into a 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'] # Draw a map of GDP per capita 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
Through the above code, we can get the per capita GDP map of countries around the world, from which we can 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 put forward corresponding policy recommendations.
# Calculate population density world_data['Population Density'] = world_data['Population'] / world_data.geometry.area # Draw 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 demonstration of this article, we have learned how to use Python and Geopandas to analyze and visualize geographic data. Geographic data analysis and visualization can help us gain a deeper understanding of the spatial distribution and characteristics of the earth, thereby 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.
Thanks for reading this article, I hope it will inspire and help you!
Summarize
This article takes an in-depth look at how to use Python and Geopandas for geographic data visualization and analysis, and provides a wealth of code examples and case demonstrations. The following is the main summary of this article:
-
Preparation : Before starting, you need to ensure that Python and Geopandas libraries have been installed. You can use pip to install Geopandas.
-
Loading geographic data : Geopandas supports multiple geographic data formats, including Shapefile, GeoJSON, Geopackage, etc. You can use
gpd.read_file()
functions to load data. -
Data exploration and processing : After loading the data, you can perform some basic exploration and processing, such as viewing the first few rows of the data, column names, data types, etc.
-
Geographic data visualization : Use the Matplotlib library to visualize geographic data, and customize the map by adjusting styles and adding labels.
-
Spatial analysis and query : Geopandas supports spatial analysis and query, such as spatial query, spatial buffer and other operations.
-
Data saving and exporting : You can use Geopandas to save geographic data into files in Shapefile, GeoJSON and other formats.
-
Data projection and coordinate conversion : Geopandas supports data projection and coordinate conversion, and can project the map into different projection methods.
-
Interactive geographical data visualization : Interactive geographical data visualization can be realized through libraries such as Bokeh and Folium, enhancing the interactivity of data exploration and display.
-
Geographic data analysis and visualization case : Through case demonstration, it shows how to use Python and Geopandas to analyze the economic and demographic situation of countries around the world and visualize the results.
-
Conclusion and outlook : Geographic data analysis and visualization are widely used in various fields. With the development of technology, it will provide us with more useful information and insights.
By studying this article, readers can master the basic methods of processing and visualizing geographic data using Python and Geopandas, and provide support and guidance for practical applications.
Click to follow and learn about Huawei Cloud’s new technologies as soon as possible~
How much revenue can an unknown open source project bring? Microsoft's Chinese AI team collectively packed up and went to the United States, involving hundreds of people. Huawei officially announced that Yu Chengdong's job changes were nailed to the "FFmpeg Pillar of Shame" 15 years ago, but today he has to thank us—— Tencent QQ Video avenges its past humiliation? Huazhong University of Science and Technology’s open source mirror site is officially open for external access report: Django is still the first choice for 74% of developers. Zed editor has made progress in Linux support. A former employee of a well-known open source company broke the news: After being challenged by a subordinate, the technical leader became furious and rude, and was fired and pregnant. Female employee Alibaba Cloud officially releases Tongyi Qianwen 2.5 Microsoft donates US$1 million to the Rust Foundation