代码
import json
from pyecharts.charts import Map
from pyecharts.options import *
f = open("./疫情.txt", "r", encoding="UTF-8")
data = f.read()
f.close()
# 取到各省份数据
# 将json字符串转为python字典,反序列化
data_dict = json.loads(data)
province_data_list = data_dict["areaTree"][0]["children"]
# 组装每个省份和确诊人数为元组,将各个省数据封装入列表
data_list = []
for province_data in province_data_list:
province_name = province_data["name"] + "省"
print(province_name)
province_confirm = province_data["total"]["confirm"]
data_list.append((province_name, province_confirm))
# 创建地图对象
map = Map()
# 添加数据
# 第三个参数标识中国地图
map.add("各省份确诊人数", data_list, "china")
# 设置全局配置,定制分段的视觉映射
map.set_global_opts(
title_opts=TitleOpts(title="全国疫情地图"),
visualmap_opts=VisualMapOpts(
is_show=True,
is_piecewise=True, # 是否分段
pieces=[
{"min":1, "max": 99, "label": "1~99", "color": "#CCFFFF"},
{"min":100, "max": 999, "label": "100~999", "color": "#FFFF99"},
{"min":1000, "max": 4999, "label": "1000~4999", "color": "#FF9966"},
{"min":5000, "max": 9999, "label": "5000~9999", "color": "#FF6666"},
{"min":10000, "max": 99999, "label": "10000~99999", "color": "#CC3333"},
{"min":100000, "label": "100000+", "color": "#990033"}
]
)
)
map.render("全国疫情地图.html")
输出结果
由于 pyecharts.charts.Map
对于省名识别现在变成了一定要加省,比如一定要写福建省而不能写福建。所以其中一些省数据没有绘制上去