How to Fix Pyecharts Map Visualization Errors in Python: A Step‑by‑Step Guide
This article walks through a follower's Pyecharts map visualization problem, explains why the original code failed, and provides a corrected Python script that successfully renders COVID‑19 data maps, complete with screenshots and full code examples.
Introduction
In this article the author, a Python enthusiast, answers a follower's question about visualizing COVID‑19 data with Pyecharts, showing why the original code failed to display data.
Original Code and Issue
The follower provided code that reads an Excel file, extracts province names and various case numbers, and attempts to create several Map charts. However, the data handling caused missing or incorrect map displays.
# 可视化部分
import pandas as pd
from pyecharts.charts import Map, Page
from pyecharts import options as opts
# 设置列对齐
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
# 打开文件
df = pd.read_excel('D:\python-base\python\实训项目文档\国内疫情统计表1.xlsx')
locations = [location for location in df['省']]
values = [value for value in df['当前确诊']]
datas1 = list(zip(locations, values))
data2 = df['省']
data2_list = list(data2)
data3 = df['当前确诊']
data3_list = list(data3)
data4 = df['疑似确诊']
data4_list = list(data4)
data5 = df['累计确诊']
data5_list = list(data5)
data6 = df['死亡人数']
data6_list = list(data6)
data7 = df['治愈人数']
data7_list = list(data7)
a = (
Map()
.add("当前确诊", datas1, "china")
.set_global_opts(
title_opts=opts.TitleOpts(),
visualmap_opts=opts.VisualMapOpts(max_=100),
)
)
b = (
Map()
.add("疑似确诊", [list(z) for z in zip(data2_list, data4_list)], "china")
.set_global_opts(
title_opts=opts.TitleOpts(),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
c = (
Map()
.add("累计确诊", [list(z) for z in zip(data2_list, data5_list)], "china")
.set_global_opts(
title_opts=opts.TitleOpts(),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
d = (
Map()
.add("死亡人数", [list(z) for z in zip(data2_list, data6_list)], "china")
.set_global_opts(
title_opts=opts.TitleOpts(),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
e = (
Map()
.add("治愈人数", [list(z) for z in zip(data2_list, data7_list)], "china")
.set_global_opts(
title_opts=opts.TitleOpts(),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
page = Page(layout=Page.DraggablePageLayout)
page.add(a, b, c, d, e)
page.render()Corrected Code
The follower revised the script to clean province names, handle special regions, and ensure the data lists match the map requirements. The updated code successfully generates the five maps.
# 可视化部分
import pandas as pd
from pyecharts.charts import Map, Page
from pyecharts import options as opts
# 设置列对齐
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
# 打开文件
df = pd.read_excel('国内疫情统计表1.xlsx')
locations = []
for location in df['省']:
if "广西" in location:
location = "广西"
if "新疆" in location:
location = "新疆"
if "宁夏" in location:
location = "宁夏"
if "西藏" in location:
location = "西藏"
if "内蒙古" in location:
location = "内蒙古"
else:
location = location.strip("省市")
locations.append(location)
values = [value for value in df['当前确诊']]
datas1 = list(zip(locations, values))
data2 = locations
data2_list = list(data2)
data3 = df['当前确诊']
data3_list = list(data3)
data4 = df['疑似确诊']
data4_list = list(data4)
data5 = df['累计确诊']
data5_list = list(data5)
data6 = df['死亡人数']
data6_list = list(data6)
data7 = df['治愈人数']
data7_list = list(data7)
a = (
Map()
.add("当前确诊", datas1, "china")
.set_global_opts(
title_opts=opts.TitleOpts(),
visualmap_opts=opts.VisualMapOpts(max_=100),
)
)
b = (
Map()
.add("疑似确诊", [list(z) for z in zip(data2_list, data4_list)], "china")
.set_global_opts(
title_opts=opts.TitleOpts(),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
c = (
Map()
.add("累计确诊", [list(z) for z in zip(data2_list, data5_list)], "china")
.set_global_opts(
title_opts=opts.TitleOpts(),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
d = (
Map()
.add("死亡人数", [list(z) for z in zip(data2_list, data6_list)], "china")
.set_global_opts(
title_opts=opts.TitleOpts(),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
e = (
Map()
.add("治愈人数", [list(z) for z in zip(data2_list, data7_list)], "china")
.set_global_opts(
title_opts=opts.TitleOpts(),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
page = Page(layout=Page.DraggablePageLayout)
page.add(a, b, c, d, e)
page.render()Result
The corrected script renders the expected maps, as shown in the screenshots below.
Conclusion
The article demonstrates how to troubleshoot and fix data‑processing issues that prevent Pyecharts maps from displaying, providing a complete, runnable example for readers facing similar problems.
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.
