全国疫情地图构建
"""
全国疫情可视化地图开发
"""
import os
import json
from pyecharts.charts import Map
from pyecharts.options import *
# 读取数据文件
f = open("D:/BaiduNetdiskDownload/资料/可视化案例数据/地图数据/疫情.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"] # 省份名称
province_confirm = province_data["total"]["confirm"] # 确诊人数
data_list.append((province_name,province_confirm))
print(data_list)
# 创建地图对象
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, "lable": "1-99人", "color": "#F5DEB3"},
{"min": 100, "max": 999, "lable": "100-999人", "color": "#F4A460"},
{"min": 1000, "max": 4999, "lable": "1000-4999人", "color": "#D2691E"},
{"min": 5000, "max": 9999, "lable": "5000-9999人", "color": "#B22222"},
{"min": 10000, "max": 99999, "lable": "10000-99999人", "color": "#B22222"},
{"min": 100000, "lable": "100000+", "color": "#800a12"},
]
)
)
# 绘图
map.render("全国疫情地图.html")
os.system("全国疫情地图.html")
河南省疫情地图绘制
"""
河南省疫情地图开发
"""
import os
import json
from pyecharts.charts import Map
from pyecharts.options import *
# 读取文件
f = open("D:/BaiduNetdiskDownload/资料/可视化案例数据/地图数据/疫情.txt","r",encoding="UTF-8")
data = f.read()
# 关闭文件
f.close()
# 获取河南省数据
# JSON数据转换为python字典
data_dict = json.loads(data)
# 去到河南省数据
cities_data = data_dict["areaTree"][0]["children"][3]["children"]
# 准备数据为元组并放入list
data_list = []
for city_data in cities_data:
city_name = city_data["name"] + "市"
city_confirm = city_data["total"]["confirm"]
data_list.append((city_name,city_confirm))
print(data_list)
# 构建地图
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, "lable": "1-99人", "color": "#F5DEB3"},
{"min": 100, "max": 999, "lable": "100-999人", "color": "#F4A460"},
{"min": 1000, "max": 4999, "lable": "1000-4999人", "color": "#D2691E"},
{"min": 5000, "max": 9999, "lable": "5000-9999人", "color": "#B22222"},
{"min": 10000, "max": 99999, "lable": "10000-99999人", "color": "#B22222"},
{"min": 100000, "lable": "100000+", "color": "#800a12"},
]
)
)
# 绘图
map.render("河南省疫情地图.html")
os.system("河南省疫情地图.html")