杰瑞科技汇

Python如何绘制中国地图?

Of course! Creating a map of China in Python is a great way to visualize geographic data. The most popular and powerful library for this task is Folium, which allows you to create interactive maps.

Here's a complete guide, starting from the simplest case and moving to more advanced examples with data visualization.

Prerequisites

First, you need to install the necessary libraries. You'll need folium for the map and geopy to get the coordinates for Beijing.

pip install folium geopy

Method 1: The Simplest Interactive Map of China

This method creates a basic, zoomable, and pannable map of China centered on its capital, Beijing.

import folium
# The approximate coordinates for the center of China
china_lat = 35.8617
china_lon = 104.1954
# Create a map object, centered on China
# We can set the initial zoom level (higher is more zoomed in)
china_map = folium.Map(location=[china_lat, china_lon], zoom_start=4)
# Save the map to an HTML file
china_map.save("china_map_simple.html")
print("Map has been saved to china_map_simple.html")

When you run this code, it will generate a file named china_map_simple.html. Open this file in your web browser, and you'll see an interactive map of China.


Method 2: Adding a Marker for Beijing

A map is more useful with markers. Let's add a marker for China's capital, Beijing.

import folium
# Coordinates for Beijing
beijing_lat = 39.9042
beijing_lon = 116.4074
# Create the map object
china_map = folium.Map(location=[35.8617, 104.1954], zoom_start=4)
# Add a marker for Beijing
# popup: The text that appears when you click the marker
# tooltip: The text that appears when you hover over the marker
folium.Marker(
    location=[beijing_lat, beijing_lon],
    popup="Beijing",
    tooltip="Click for more info",
    icon=folium.Icon(color="red", icon="star", prefix='fa')
).add_to(china_map)
# Save the map
china_map.save("china_map_with_marker.html")
print("Map has been saved to china_map_with_marker.html")

This will create a map with a distinctive red star marker for Beijing.


Method 3: Choropleth Map - Visualizing Provincial Data

This is where Folium truly shines. A choropleth map is a thematic map in which areas are shaded in proportion to a statistical variable. We'll use this to visualize data by province.

For this, we need two things:

  1. GeoJSON Data: A file that defines the geographic boundaries of each province in a format computers can understand.
  2. Data: A CSV file with the data we want to map (e.g., population, GDP).

Step 1: Get the Files

  1. GeoJSON File: You can find a GeoJSON file for Chinese provinces online. A reliable source is the Natural Earth dataset. For this example, we'll use a pre-made one. You can download it from here:

  2. CSV Data File: Let's create a simple CSV file named china_population_data.csv with the estimated population for each province.

    china_population_data.csv

    name,population
    Anhui,61027
    Beijing,2154,
    Chongqing,3124,
    Fujian,3941,
    Gansu,2637,
    Guangdong,12601,
    Guangxi,5013,
    Guizhou,3856,
    Hainan,1008,
    Hebei,7556,
    Heilongjiang,3185,
    Henan,9937,
    Hubei,5775,
    Hunan,6918,
    Jiangsu,8475,
    Jiangxi,4649,
    Jilin,2407,
    Liaoning,4359,
    Inner Mongolia,2520,
    Ningxia,720,
    Qinghai,603,
    Shaanxi,3954,
    Shandong,10152,
    Shanghai,2487,
    Shanxi,3729,
    Sichuan,8367,
    Tianjin,1387,
    Tibet,364,
    Xinjiang,2585,
    Yunnan,4830,
    Zhejiang,6457,
    Hong Kong,750,
    Macau,68,
    Taiwan,2360

    Note: Population is in thousands.

Step 2: Write the Python Code

Now, we'll use pandas to read the CSV data and folium to create the choropleth map.

import folium
import pandas as pd
# Create a base map
china_map = folium.Map(location=[35.8617, 104.1954], zoom_start=4)
# Load the data
geo_data = 'china-provinces.geojson' # Path to your GeoJSON file
population_data = pd.read_csv('china_population_data.csv')
# Create a choropleth map
# The key is to match the 'name' in the GeoJSON with the 'name' in the CSV
folium.Choropleth(
    geo_data=geo_data,
    data=population_data,
    columns=['name', 'population'], # This pairs the GeoJSON key with the data value
    key_on='feature.properties.name', # This tells Folium where to find the province name in the GeoJSON
    fill_color='YlOrRd', # Color scheme (Yellow-Orange-Red)
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name='Population by Province (in thousands)',
    highlight=True
).add_to(china_map)
# Save the map
china_map.save("china_map_choropleth.html")
print("Choropleth map has been saved to china_map_choropleth.html")

When you open china_map_choropleth.html, you will see a color-coded map where provinces with higher populations (like Guangdong and Shandong) are darker.

Summary of Libraries and Methods

Method Library(s) Use Case Complexity
Simple Map folium Displaying a basic, interactive map. Low
Marker Map folium Highlighting specific points of interest (e.g., cities). Low
Choropleth Map folium, pandas Visualizing statistical data across geographic regions (e.g., provinces). Medium
Static Map geopandas, matplotlib Creating high-quality, non-interactive images for reports or papers. Medium

For most interactive web-based visualizations, Folium is the best and easiest choice in Python.

分享:
扫描分享到社交APP
上一篇
下一篇