Animating China’s 500+ Million Population Cities with Python & Geopandas
This article explains how to identify Chinese prefecture‑level cities with populations over five million, classify them by tier, and create an animated bubble map using Python libraries such as geopandas, pandas, matplotlib and imageio, finally exporting the result as an HTML page and GIF.
Excluding Hong Kong, Macau and Taiwan, China has 337 prefecture‑level cities. By considering population size and economic development, these cities are grouped into first‑tier, new‑first‑tier, second‑tier, third‑tier, fourth‑tier and fifth‑tier categories.
According to the latest census data, 88 of these cities have populations exceeding five million. The following Python script dynamically visualizes these megacities on a map, drawing provincial boundaries, the nine‑dash line, and animated bubbles whose size reflects population.
Key steps include loading GeoJSON files for provinces, the South China Sea line, and city points, converting coordinates, and preparing a DataFrame with x, y, and population values. The bubble_map_dance function creates a Matplotlib figure, draws base layers, plots scatter points with a color map, annotates selected cities, and animates the growth of bubbles across frames.
import numpy as
import pandas as
import geopandas as gpd
import shapely
from shapely import geometry as geo
from shapely import wkt
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import contextily as ctx
import imageio
import os
from PIL import Image
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['animation.writer'] = 'html'
plt.rcParams['animation.embed_limit'] = 100
# ... (full code as provided in the source) ...
html_file = "中国超500万人口城市.html"
bubble_map_dance(df, filename=html_file)
gif_file = html_file.replace('.html', '.gif')
html_to_gif(html_file, gif_file, duration=0.5)The script generates an interactive HTML animation and converts it to a GIF, enabling a clear visual comparison of the most populous Chinese cities.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
