案例:河南省疫情地图
import json
from pyecharts.charts import Map
from pyecharts.options import *
# 读取文件
f = open("C:\\学习\\疫情.txt", "r", encoding="UTF-8")
data = f.read()
# 关闭文件
f.close()
# 将json转换成字典
data_dict = json.loads(data)
# 取省份数据
cities_data_list = data_dict["areaTree"][0]["children"][3]["children"]
#将数据为元组放入list
data_list = []
for city_data in cities_data_list:
city_name = city_data["name"] +"市" # 省份名称
city_confirm = city_data["total"]["confirm"] # 确诊人数
data_list.append((city_name, city_confirm))
map = Map()
# 添加数据
map.add("河南确诊人数", data_list, "河南")
# 配置全局配置
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": "1~99", "color": "#FFFF99"},
{"min": 1000, "max": 4999, "label": "1~99", "color": "#FF9966"},
{"min": 5000, "max": 9999, "label": "1~99", "color": "#FF6666"},
{"min": 10000, "max": 99999, "label": "1~99", "color": "#CC3333"},
{"min": 100000, "max": 99999999999, "label": "1~99", "color": "#990033"},
]
)
)
# 绘图
map.render("河南疫情地图.html")
得到