地图可视化
- 1.基础地图使用
- 1.1基础地图演示
- 1.2视觉映射器
- 2.全国疫情地图
- 2.1数据整理
- 2.2创建地图并添加数据
- 2.3设置全局配置
- 3.省级疫情图
1.基础地图使用
1.1基础地图演示
# 导入模块
from pyecharts.charts import Map
# 绘图
map = Map()
# 构建数据
data = [
("北京市",99),
("上海市",199),
("湖南省",299),
("台湾省",199),
("安徽省",299),
("广州省",399),
("湖北省",599)
]
map.add("地图",data,"china")
map.render("基础地图.html")
1.2视觉映射器
# 导入模块
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
# 绘图
map = Map()
# 构建数据
data = [
("北京市",99),
("上海市",199),
("湖南省",299),
("台湾省",199),
("安徽省",299),
("广州省",399),
("湖北省",599)
]
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("基础地图.html")
2.全国疫情地图
2.1数据整理
# 导入json模块
import json
# 读取文件
f = open("D:/疫情.txt","r",encoding="UTF-8")
data = f.read() # 获取全部数据
# 关闭文件
f.close()
# 将字符串json转换成python的字典
data_dict = json.loads(data) # 基础数据字典
# 取到各省数据
# 将字符串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))
2.2创建地图并添加数据
# 绘制地图对象
map = Map()
# 添加数据
map.add("各省份确诊人数",data_list,"china")
2.3设置全局配置
# 设置全局配置,定制分段的视觉映射
map.set_global_opts(
title_opts=TitleOpts(title="全国疫情图"),
visualmap_opts=VisualMapOpts(
is_show=True, # 是否显示
is_piecewise=True, # 是否分段
pieces=[
{"min":1,"max":9,"label":"1-9","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")
3.省级疫情图
import json
from pyecharts.charts import Map
from pyecharts.options import *
f = open("D:/疫情.txt","r",encoding="UTF-8")
data = f.read()
data = json.loads(data)
Tianjin_data_list = data["areaTree"][0]["children"][13]["children"]
data_list = []
for Tianjin_data in Tianjin_data_list:
name = Tianjin_data["name"]
confirm = Tianjin_data["total"]["confirm"]
data_list.append((name,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":9,"label":"1-9","color":"#CCFFFF"},
{"min":10,"max":99,"label":"100-999","color":"#FFFF99"},
{"min":100,"max":500,"label":"1000-4999","color":"#FF9966"},
{"min":501,"max":999,"label":"5000-9999","color":"#FF6666"},
{"min":10000,"label":"10000-99999","color":"#CC3333"}
]
)
)
map.render("省级疫情地图.html")