Create Interactive COVID‑19 Maps and Timelines with Python pyecharts
This tutorial shows how to fetch real‑time COVID‑19 data from Baidu, then use Python's pyecharts library to build province‑level maps and a dynamic timeline carousel, complete with code snippets and visual results.
Hello, I am a Python enthusiast sharing a practical visualization project.
Preface
A student asked for help with a visualization assignment, which led to creating a carousel of maps. The article references Huang Wei's work on cool migration and carousel charts using pyecharts.
Data Source
The data comes from Baidu's real‑time COVID‑19 big‑data report.
Implementation Process
Newly Confirmed Cases
Code to generate a map of newly confirmed cases in Shaanxi province:
from pyecharts.charts import Map, Timeline
from pyecharts import options as opts
# 准数据
shanxi_city = ["西安市", "延安市", "咸阳市", "渭南市", "安康市", "汉中市", "宝鸡市", "铜川市", "商洛市", "榆林市", "韩城市", "杨凌示范区"]
shanxi_data = [46, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# 绘制陕西疫情地图
map = (
Map()
.add('陕西省', [(i, j) for i, j in zip(shanxi_city, shanxi_data)], '陕西')
.set_global_opts(title_opts=opts.TitleOpts(title='陕西省新增感染病例疫情图'), visualmap_opts=opts.VisualMapOpts(max_=50, is_piecewise=True))
)
# 渲染数据
map.render('陕西省新增感染病例疫情图.html')Resulting map:
Existing Cases
Map of current cases (image only):
Cumulative Cases
Map of cumulative cases (image only):
Recovered Cases
Map of recovered cases (image only):
Deaths
Map of death cases (image only):
Carousel (Timeline)
Code to combine the five maps into an interactive timeline carousel:
from pyecharts.charts import Map, Timeline
from pyecharts import options as opts
# 1. 准数据
shanxi_city = ["西安市", "延安市", "咸阳市", "渭南市", "安康市", "汉中市", "宝鸡市", "铜川市", "商洛市", "榆林市", "韩城市", "杨凌示范区"]
xinzeng = [46, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
xianyou = [1747, 13, 11, 1, 0, 0, 0, 0, 0, 0, 0, 0]
leiji = [2094, 21, 31, 18, 26, 26, 13, 8, 7, 3, 1, 1]
zhiyu = [304, 8, 20, 17, 26, 26, 13, 8, 7, 3, 1, 1]
siwang = [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# 2. 绘制新增疫情地图:格式一
map1 = (
Map(init_opts=opts.InitOpts(width="700px", height="300px", theme="blue"))
.add('新增病例', [(i, j) for i, j in zip(shanxi_city, xinzeng)], '陕西')
.set_global_opts(visualmap_opts=opts.VisualMapOpts(max_=50))
)
# 3. 绘制现有疫情地图:格式二
map2 = (
Map()
.add('现有病例', [(i, j) for i, j in zip(shanxi_city, xianyou)], '陕西')
.set_global_opts(visualmap_opts=opts.VisualMapOpts(max_=1750, is_piecewise=True))
)
# 4. 绘制累计疫情地图:格式三
map3 = (
Map()
.add('累计病例', [(i, j) for i, j in zip(shanxi_city, leiji)], '陕西')
.set_global_opts(visualmap_opts=opts.VisualMapOpts(max_=2100, is_piecewise=True))
)
# 5. 绘制治愈疫情地图:格式四
map4 = (
Map()
.add('治愈病例', [(i, j) for i, j in zip(shanxi_city, zhiyu)], '陕西')
.set_global_opts(visualmap_opts=opts.VisualMapOpts(max_=310, is_piecewise=True))
)
# 6. 绘制死亡疫情地图:格式五
map5 = (
Map()
.add('死亡病例', [(i, j) for i, j in zip(shanxi_city, siwang)], '陕西')
.set_global_opts(visualmap_opts=opts.VisualMapOpts(max_=3, is_piecewise=True))
)
# 7. 创建组合类对象
timeline = Timeline(init_opts=opts.InitOpts(width='720px', height='350px'))
# 8. 在组合对象中添加需要组合的图表对象
timeline.add(chart=map1, time_point="陕西省新增病例疫情图")
timeline.add(chart=map2, time_point="陕西省现有病例疫情图")
timeline.add(chart=map3, time_point="陕西省累计病例疫情图")
timeline.add(chart=map4, time_point="陕西省治愈病例疫情图")
timeline.add(chart=map5, time_point="陕西省死亡病例疫情图")
timeline.add_schema(is_auto_play=True, play_interval=2000)
# 9. 渲染数据
timeline.render('陕西省疫情轮播图.html')Resulting carousel image:
Summary
This article demonstrates how to obtain COVID‑19 data from Baidu's real‑time report and use Python's pyecharts library to create province‑level maps and an animated timeline carousel, providing complete code and visual results for learners.
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.
