python中利用pyecharts进行地图可视化,查看北京市的疫情各区情况,结果为动态图鼠标单击可查看具体人数(已知数据条件下):
import json
from pyecharts import options as opts
from pyecharts.charts import Map
# 需求: 获取 北京 信息
# 1 使用 with open 读取 疫情.txt 的数据
from pyecharts.faker import Faker
with open('疫情.txt', 'r', encoding="utf-8") as f:
data = f.read()
# 2 将数据 转化成 json 格式
data = json.loads(data)
# 3 获取 children 的省级信息
data = data['areaTree'][0]['children']
# 4 遍历 市级信息 列表
for p in data:
# 4.1 如果是北京, 将 市内的市级数据(children)封装到data中
# print(p['name'])
if p['name'] == '北京':
data = p['children']
break
# 5 声明 市级字典 变量
citys_dict = {}
# 6 遍历 市级信息 列表
for c in data:
# 6.1 如果 不是 境外输入 且 不是 地区待确认, 就把 市名称 和 确证信息 封装 字典中
if c['name']!='境外输入' and c['name']!='地区待确认':
# print(c['name'])
# print(c['total']['confirm'])
citys_dict[c['name'] + "区"] = c['total']['confirm']
# 7 使用 list[zip(keys, vals)]将数据封装列表 [(), ()] 中
data = list(zip(citys_dict.keys(), citys_dict.values()))
# 8 打印测试
#print(data)
c = (
Map()
.add("北京市疫情", data, "北京")
.set_global_opts(
title_opts=opts.TitleOpts(title="北京市疫情_主标题"),
visualmap_opts=opts.VisualMapOpts(
is_piecewise=True,
pieces=[
{"min": 1, "max": 9, "label": '1~9人'},
{"min": 10, "max": 99, "label": '10~99人'},
{"min": 100, "max": 999, "label": '100~999人'},
{"min": 1000, "max": 9999, "label": '1000~9999人'},
{"min": 10000, "label": '10000人以上'},
]
)
)
.render("henan_virus_map.html")
)



