演示地图可视化的基本使用
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
# 准备地图对象
map = Map()
# 准备数据
data = [
("北京市", 99),
("上海市", 199),
("湖南省", 299),
("台湾省", 399),
("广东省", 499)
]
print(data)
# 添加数据
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": "#FFFF99"},
{"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": "10000以上", "coLor": "#990033"}
])
)
# 绘图
map.render("./bak/测试地图.html")
[('北京市', 99), ('上海市', 199), ('湖南省', 299), ('台湾省', 399), ('广东省', 499)]
'd:\\git-python\\bak\\测试地图.html'
全国疫情地图
import json
from pyecharts.charts import Map
from pyecharts.options import TitleOpts, VisualMapOpts
# 读取数据文件
f = open("./bak/疫情.txt", "r", encoding="UTF-8")
data = f.read()
# print(data)
# 关闭文件
f.close()
# 取到各省数据,将字符串转换为python的字典
data_dict = json.loads(data)
print(data_dict)
province_data_list = data_dict['areaTree'][0]['children']
print(province_data_list)
# 组装每个省份和确诊人数为元组,并将各种省的数据都封装入列表内
data_list = list()
for i in province_data_list:
if i['name'] in {"上海", "重庆", "北京", "天津"}:
province_name = i['name']+"市"
elif i['name'] in ["香港", "澳门"]:
province_name = i['name']+"特别行政区"
elif i['name'] in ["内蒙古", "西藏"]:
province_name = i['name']+"自治区"
elif i['name'] in ["新疆"]:
province_name = i['name']+"维吾尔自治区"
elif i['name'] in ["宁夏"]:
province_name = i['name']+"回族自治区"
elif i['name'] in ["广西"]:
province_name = i['name']+"壮族自治区"
else:
province_name = i['name']+"省"
province_confirm = i['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, "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("./bak/全国疫情地图.html")
{'lastUpdateTime': '2021-08-18 10:53:30', 'chinaTotal'...省略...
[('台湾省', 15880), ('江苏省', 1576), ('云南省', 982), ('河南省', 1518), ('上海市', 1518), ('湖南省', 1181), ('湖北省', 68286), ('广东省', 2978), ('香港特别行政区', 2978), ('福建省', 773), ('浙江省', 1417), ('山东省', 923), ('四川省', 1179), ('天津市', 1179), ('北京市', 1179), ('陕西省', 668), ('广西壮族自治区', 668), ('辽宁省', 441), ('重庆市', 441), ('澳门特别行政区', 441), ('甘肃省', 199), ('山西省', 255), ('海南省', 190), ('内蒙古自治区', 190), ('吉林省', 574), ('黑龙江省', 1613), ('宁夏回族自治区', 1613), ('青海省', 18), ('江西省', 937), ('贵州省', 147), ('西藏自治区', 147), ('安徽省', 1008), ('河北省', 1317), ('新疆维吾尔自治区', 1317)]
'd:\\git-python\\bak\\全国疫情地图.html'
河南省疫情地图
import json
from pyecharts.charts import Map
from pyecharts.options import TitleOpts, VisualMapOpts
# 读取数据文件
f = open("./bak/疫情.txt", "r", encoding="UTF-8")
data = f.read()
# print(data)
# 关闭文件
f.close()
# 取到各省数据,将字符串转换为python的字典
data_dict = json.loads(data)
# print(data_dict)
henan_data_list = data_dict['areaTree'][0]['children'][3]["children"]
print(henan_data_list)
# 组装每个省份和确诊人数为元组,并将各种省的数据都封装入列表内
data_list = list()
for i in henan_data_list:
city_name = i['name']+"市"
city_confirm = i['total']['confirm']
data_list.append((city_name, city_confirm))
data_list.append(("济源市", 5))
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": "10-99人", "color": "#FFFF99"},
{"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": "10000以上", "coLor": "#990033"}
]
)
)
# 绘图
map.render("./bak/河南省疫情地图.html")
[{'name': '郑州', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'nowConfirm': 135, 'confirm': 295, 'suspect': 0, 'dead': 5, 'deadRate': '1.69', 'showRate': False, 'heal': 155, 'healRate': '52.54', 'showHeal': True, 'grade': '部分中高风险', 'wzz': 0}}, {'name': '境外输入', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'nowConfirm': 32, 'confirm': 79, 'suspect': 0, 'dead': 0, 'deadRate': '0.00', 'showRate': False, 'heal': 47, 'healRate': '59.49', 'showHeal': True, 'wzz': 0}}, {'name': '商丘', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'nowConfirm': 15, 'confirm': 106, 'suspect': 0, 'dead': 3, 'deadRate': '2.83', 'showRate': False, 'heal': 88, 'healRate': '83.02', 'showHeal': True, 'grade': '部分中高风险', 'wzz': 0}}, {'name': '开封', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 7, 'confirm': 33, 'suspect': 0, 'dead': 0, 'deadRate': '0.00', 'showRate': False, 'heal': 26, 'healRate': '78.79', 'showHeal': True, 'grade': '部分中高风险', 'wzz': 0}}, {'name': '驻马店', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 4, 'confirm': 143, 'suspect': 0, 'dead': 0, 'deadRate': '0.00', 'showRate': False, 'heal': 139, 'healRate': '97.20', 'showHeal': True, 'grade': '部分中高风险', 'wzz': 0}}, {'name': '安阳', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 1, 'confirm': 54, 'suspect': 0, 'dead': 0, 'deadRate': '0.00', 'showRate': False, 'heal': 53, 'healRate': '98.15', 'showHeal': True, 'grade': '全部低风险', 'wzz': 0}}, {'name': '许昌', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 1, 'confirm': 40, 'suspect': 0, 'dead': 1, 'deadRate': '2.50', 'showRate': False, 'heal': 38, 'healRate': '95.00', 'showHeal': True, 'grade': '部分中风险', 'wzz': 0}}, {'name': '三门峡', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 0, 'confirm': 7, 'suspect': 0, 'dead': 1, 'deadRate': '14.29', 'showRate': False, 'heal': 6, 'healRate': '85.71', 'showHeal': True, 'wzz': 0}}, {'name': '漯河', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 0, 'confirm': 36, 'suspect': 0, 'dead': 0, 'deadRate': '0.00', 'showRate': False, 'heal': 36, 'healRate': '100.00', 'showHeal': True, 'wzz': 0}}, {'name': '周口', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 0, 'confirm': 76, 'suspect': 0, 'dead': 1, 'deadRate': '1.32', 'showRate': False, 'heal': 75, 'healRate': '98.68', 'showHeal': True, 'wzz': 0}}, {'name': '南阳', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 0, 'confirm': 156, 'suspect': 0, 'dead': 3, 'deadRate': '1.92', 'showRate': False, 'heal': 153, 'healRate': '98.08', 'showHeal': True, 'wzz': 0}}, {'name': '信阳', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 0, 'confirm': 274, 'suspect': 0, 'dead': 2, 'deadRate': '0.73', 'showRate': False, 'heal': 272, 'healRate': '99.27', 'showHeal': True, 'wzz': 0}}, {'name': '济源示范区', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 0, 'confirm': 5, 'suspect': 0, 'dead': 0, 'deadRate': '0.00', 'showRate': False, 'heal': 5, 'healRate': '100.00', 'showHeal': True, 'wzz': 0}}, {'name': '新乡', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 0, 'confirm': 57, 'suspect': 0, 'dead': 3, 'deadRate': '5.26', 'showRate': False, 'heal': 54, 'healRate': '94.74', 'showHeal': True, 'wzz': 0}}, {'name': '焦作', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 0, 'confirm': 32, 'suspect': 0, 'dead': 1, 'deadRate': '3.12', 'showRate': False, 'heal': 31, 'healRate': '96.88', 'showHeal': True, 'wzz': 0}}, {'name': '平顶山', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 0, 'confirm': 58, 'suspect': 0, 'dead': 1, 'deadRate': '1.72', 'showRate': False, 'heal': 57, 'healRate': '98.28', 'showHeal': True, 'wzz': 0}}, {'name': '洛阳', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 0, 'confirm': 31, 'suspect': 0, 'dead': 1, 'deadRate': '3.23', 'showRate': False, 'heal': 30, 'healRate': '96.77', 'showHeal': True, 'wzz': 0}}, {'name': '濮阳', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 0, 'confirm': 17, 'suspect': 0, 'dead': 0, 'deadRate': '0.00', 'showRate': False, 'heal': 17, 'healRate': '100.00', 'showHeal': True, 'wzz': 0}}, {'name': '鹤壁', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': False}, 'total': {'nowConfirm': 0, 'confirm': 19, 'suspect': 0, 'dead': 0, 'deadRate': '0.00', 'showRate': False, 'heal': 19, 'healRate': '100.00', 'showHeal': True, 'wzz': 0}}, {'name': '地区待确认', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'nowConfirm': -4, 'confirm': 0, 'suspect': 0, 'dead': 0, 'deadRate': '0.00', 'showRate': False, 'heal': 4, 'healRate': '0.00', 'showHeal': True, 'wzz': 0}}]
[('郑州市', 295), ('境外输入市', 79), ('商丘市', 106), ('开封市', 33), ('驻马店市', 143), ('安阳市', 54), ('许昌市', 40), ('三门峡市', 7), ('漯河市', 36), ('周口市', 76), ('南阳市', 156), ('信阳市', 274), ('济源示范区市', 5), ('新乡市', 57), ('焦作市', 32), ('平顶山市', 58), ('洛阳市', 31), ('濮阳市', 17), ('鹤壁市', 19), ('地区待确认市', 0), ('济源市', 5)]
'd:\\git-python\\bak\\河南省疫情地图.html'