How to Fix Pyecharts Map Visualization Issues with COVID Data in Python
This article walks through a follower's Pyecharts map problem caused by improper province name handling, presents the original and revised Python code, and shows how correcting the data preprocessing resolves the missing visualization, providing a clear solution for Python developers.
Introduction
In response to a follower's question about a Pyecharts map that displayed no data, the author explains the issue and shares the original code.
Original Code
# Visualization part
import pandas as pd
from pyecharts.charts import Map, Page
from pyecharts import options as opts
# Set column alignment
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
# Open file
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)
# Generate render.html
page.render()
# After rendering, comment out page.render() and resize HTML
Page.save_resize_html("render.html", cfg_file="chart_config.json", dest="my_test.html")Revised Code
# Visualization part
import pandas as pd
from pyecharts.charts import Map, Page
from pyecharts import options as opts
# Set column alignment
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
# Open file
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['当前确诊']]
print(values, locations)
datas1 = list(zip(locations, values))
data2 = locations
data2_list = list(data2)
print(data2_list)
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)
# Generate render.html
page.render()
# After rendering, comment out page.render() and resize HTML
Page.save_resize_html("render.html", cfg_file="chart_config.json", dest="my_test.html")The revised code correctly normalizes province names, fixes the data alignment, and the map now displays the COVID‑19 statistics as intended.
Conclusion
This article demonstrates how to troubleshoot a Pyecharts map visualization problem by cleaning the source data and adjusting the Python code, providing a practical solution for developers working with data visualization in Python.
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.
