How to Visualize Chinese Provincial GDP with Python and pyecharts
This tutorial shows how to use Python's pyecharts library to create geographic visualizations of Chinese provincial GDP, covering single‑year maps, piecewise color segmentation, and animated timelines for multiple years, with full code examples and data preparation steps.
Introduction
When comparing regional indicators, histograms often fail to highlight differences, so geographic visualization using maps and color gradients can be more effective. This article demonstrates how to use the Python library pyecharts to draw Chinese provincial GDP maps for a single year and for a series of years (2010‑2019).
Data preparation
The GDP data are downloaded from the National Bureau of Statistics and saved as CSV or Excel files. They are loaded into a pandas.DataFrame with pd.read_csv(..., encoding='GBK').
1. Visualizing a single year
Using pyecharts.charts.Map, the add method loads the province names and the GDP values for a chosen year. set_global_opts with visualmap_opts=opts.VisualMapOpts(min_=500, max_=12000) defines the color scale. The map is rendered to an HTML file.
import pandas as pd
from pyecharts.charts import Map
import pyecharts.options as opts
frame = pd.read_csv('C:\Users\dell\Desktop\分省年度数据2.csv', encoding='GBK')
map = Map()
map.add("我国地区的GDP", frame[['地区','2019年']].values.tolist(), "china")
map.set_global_opts(visualmap_opts=opts.VisualMapOpts(min_=500, max_=12000))
map.render("2019年全国各地区GDP.html")The resulting map highlights provinces with colors corresponding to their GDP values.
Piecewise color segmentation
Because GDP distribution is uneven, is_piecewise=True with a custom pieces list can assign distinct colors to defined value ranges.
geo.set_global_opts(
visualmap_opts=opts.VisualMapOpts(
is_piecewise=True,
pieces=[
{"min":0,"max":10000,"label":"1~10000","color":"cyan"},
{"min":10001,"max":20000,"label":"10001~20000","color":"yellow"},
{"min":20001,"max":50000,"label":"20001~50000","color":"orange"},
{"min":50001,"max":80000,"label":"50001~80000","color":"coral"},
{"min":80001,"max":120000,"label":"80001~120000","color":"red"},
],
)
)2. Visualizing multiple years
To create an animated timeline of GDP maps from 2010 to 2019, a for loop builds a Map for each year, sets the title, applies the same piecewise visual map, and adds the map to a Timeline. The timeline is then rendered.
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Map, Timeline
frame = pd.read_csv('C:\Users\dell\Desktop\分省年度数据2.csv', encoding='GBK')
tl = Timeline()
for i in range(2010, 2020):
map0 = (
Map()
.add("省份", frame[['地区', str(i)+'年']].values.tolist(), "china")
.set_global_opts(
title_opts=opts.TitleOpts(title=f"Map-{i}年GDP(亿元)"),
visualmap_opts=opts.VisualMapOpts(
is_piecewise=True,
pieces=[
{"min":0,"max":10000,"label":"1~10000","color":"cyan"},
{"min":10001,"max":20000,"label":"10001~20000","color":"yellow"},
{"min":20001,"max":50000,"label":"20001~50000","color":"orange"},
{"min":50001,"max":80000,"label":"50001~80000","color":"coral"},
{"min":80001,"max":120000,"label":"80001~120000","color":"red"},
],
),
)
)
tl.add(map0, f"{i}年")
tl.render("2010~2019年全国各地区GDP.html")Conclusion
The implementation is straightforward: by adapting the official pyecharts examples, beginners can quickly master geographic data visualization and extend it to their own projects.
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.
