如何添加颜色表示层级
代码实现
"""
基础地图的使用
"""
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
# 准备地图对象
map = Map()
# 准备数据
data = [
("北京市", 9),
("上海市", 59),
("甘肃省", 812),
("黑龙江省", 313),
("四川省", 1999),
("台湾省", 19999)
]
# 添加数据
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": "#38f55c"},
{"min": 10, "max": 99, "label": "10-99", "color": "#f5ad38"},
{"min": 100, "max": 499, "label": "100-500", "color": "#f54a38"},
{"min": 500, "max": 998, "label": "500-998", "color": "#9d38f5"},
{"min": 999, "label": ">999", "color": "#201641"}
]
)
)
# 绘图
map.render()
代码实现
"""
演示全国疫情可视化地图开发
"""
import json
from pyecharts.charts import Map
from pyecharts.options import *
# 读取数据文件
f = open("D:\\IOText\\DataDoing\\疫情.txt", "r", encoding="UTF-8")
china_data_json = f.read()
# 关闭文件
f.close()
# 取到各省数据
china_data_dict = json.loads(china_data_json)
province_data_list = china_data_dict["areaTree"][0]["children"]
# 地图最终所需的数据
map_list = list()
# 组装每一省份和确诊人数为元组,并各个省的数据都封装入列表内
for province_data in province_data_list:
# 省份名称
province_name = province_data["name"]
# 确诊人数
province_confirm = province_data["total"]["confirm"]
map_list.append((province_name, province_confirm))
print(map_list)
# 创建地图对象
map = Map()
# 添加数据
map.add("全国身份确诊人数", map_list, "china")
# 设置全局配置,指定分段的视觉映射
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": "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")
相关数据文件在文章开头出获取
但是我直接演示四川的地图
代码示例
"""
省级疫情地图
"""
import json
from pyecharts.charts import Map
from pyecharts.options import *
# 读取数据文件
f = open("D:\\IOText\\DataDoing\\疫情.txt", "r", encoding="UTF-8")
sichuan_data_json = f.read()
# 关闭文件
f.close()
# 取到各省数据
sichuan_data_dict = json.loads(sichuan_data_json)
sichuan_children_data_list = sichuan_data_dict["areaTree"][0]["children"][12]["children"]
# 地图最终所需的数据
map_list = list()
# 组装每一省份和确诊人数为元组,并各个省的数据都封装入列表内
for province_data in sichuan_children_data_list:
# 省份名称
if province_data["name"] == "阿坝":
province_name = province_data["name"] + "藏族羌族自治州"
elif province_data["name"] == "甘孜":
province_name = province_data["name"] + "藏族自治州"
elif province_data["name"] == "凉山":
province_name = province_data["name"] + "彝族自治州"
else:
province_name = province_data["name"] + "市"
# 确诊人数
province_confirm = province_data["total"]["confirm"]
map_list.append((province_name, province_confirm))
print(map_list)
# 创建地图对象
map = Map()
# 添加数据
map.add("全国身份确诊人数", map_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": "#57fa66"},
{"min": 10, "max": 99, "label": "10-99", "color": "#faf857"},
{"min": 100, "max": 499, "label": "100-499", "color": "#FF9966"},
{"min": 500, "max": 999, "label": "500-999", "color": "#FF6666"},
{"min": 1000, "max": 9999, "label": "1000-9999", "color": "#CC3333"},
{"min": 10000, "label": ">9999", "color": "#990033"}
]
)
)
# 绘图
map.render("四川疫情地图.html")
结果示例
简简单单直接拿下啦!!!
再见ヾ( ̄▽ ̄)Bye~Bye~