一、基础地图使用
1、基础地图演示
2、基础地图演示——视觉映射器
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts# 准备地图对象
map = Map()
# 准备数据
data = [("北京市", 99),("上海市", 199),("湖南省", 299),("台湾省", 399),("广东省", 499),
]
# 添加数据
map.add("测试地图", data, "china")
# 设置全局选项
map.set_global_opts(visualmap_opts=VisualMapOpts( # 视觉映像is_show=True,is_piecewise=True, # 设置分段pieces=[{"min": 1, "max": 9, "label": "1-9人", "color": "#CCFFFF"},{"min": 10, "max": 99, "label": "10-99人", "color": "#FF6666"},{"min": 100, "max": 500, "label": "100-500人", "color": "#990033"}])
)
# 绘图
map.render()
生成的地图链接(PC端打开):http://localhost:63342/pythonProject/render.html?_ijt=rkocdgrhhojeq0tqfkouf7hmco&_ij_reload=RELOAD_ON_SAVE
二、全国疫情地图构建
案例效果:
import json
from pyecharts.charts import Map
from pyecharts.options import TitleOpts, VisualMapOpts# 读取数据文件
f = open("D:/疫情.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))
# 创建地图对象
map = Map()
# 添加数据
map.add("个省份确诊人数", data_list, "china")
# 设置全局配置,定制分段的视觉映像
map.set_global_opts(title_opts=TitleOpts(title="全国疫情地图", pos_left="center", pos_bottom="1%"),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")
生成的地图链接(PC端打开):http://localhost:63342/pythonProject/%E5%85%A8%E5%9B%BD%E7%96%AB%E6%83%85%E5%9C%B0%E5%9B%BE.html?_ijt=2pd19lncoe59qal119povfdrvr&_ij_reload=RELOAD_ON_SAVE
三、河南省疫情地图构建
import json
from pyecharts.charts import Map
from pyecharts.options import *# 读取文件
f = open("D:\疫情.txt", "r", encoding="utf-8")
data = f.read()
# 关闭文件
f.close()
# 将json数据转化为python数据
data_dict = json.loads(data)
# 获取陕西省数据
province_shannxi_list = data_dict["areaTree"][0]["children"][15]["children"]
# 准备数据为元组,并放入list
data_list = []
for province_data in province_shannxi_list:city_name = province_data["name"] # 获取城市名称city_confirm = province_data["total"]["confirm"] # 获取确诊人数data_list.append((city_name, city_confirm)) # 放入列表
# 构建地图
map = Map()
# 添加数据
map.add("各城市确诊人数", data_list, "陕西")
# 设置全局配置
map.set_global_opts(title_opts=TitleOpts(title="陕西省疫情地图", pos_left="center", pos_bottom="1%"),visualmap_opts=VisualMapOpts(is_show=True,is_piecewise=True,pieces=[{"min": 1, "max": 9, "label": "1-9人", "color": "#CCFFFF"},{"min": 10, "max": 29, "label": "10-29人", "color": "#FFFF99"},{"min": 30, "max": 59, "label": "30-59人", "color": "#FF9966"},{"min": 60, "max": 89, "label": "60-89人", "color": "#FF6666"},{"min": 90, "max": 119, "label": "90-119人", "color": "#CC3333"},{"min": 120, "label": "120+", "color": "#990033"}])
)
# 绘图
map.render("陕西省疫情地图.html")