赞
踩
掌握使用pyecharts构建基础的地图可视化图表
课程链接
点击链接保存疫情.txt
疫情.txt
""" 演示地图可视化的基本使用 """ 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()
""" 演示全国疫情可视化地图开发 """ import json from pyecharts.charts import Map from pyecharts.options import * # 读取数据文件 f = open("E:/疫情.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"] if province_name == "北京" or province_name == "天津" or province_name == "上海" or province_name == "重庆": data_list.append((province_name + "市", province_confirm)) elif province_name == "西藏" or province_name == "内蒙古": data_list.append((province_name + "自治区", province_confirm)) elif province_name == "广西": data_list.append((province_name + "壮族自治区", province_confirm)) elif province_name == "宁夏": data_list.append((province_name + "回族自治区", province_confirm)) elif province_name == "新疆": data_list.append((province_name + "维吾尔自治区", province_confirm)) elif province_name=="澳门" or province_name=="香港": data_list.append((province_name + "特别行政区", province_confirm)) else: data_list.append((province_name + "省", province_confirm)) # 确诊人数 # data_list.append((province_name, province_confirm)) # 创建地图对象 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": "#CCFFFF"}, {"min": 100, "max": 999, "lable": "100~9999人", "color": "#FFFF99"}, {"min": 1000, "max": 4999, "lable": "1000~4999人", "color": "#FF9966"}, {"min": 5000, "max": 9999, "lable": "5000~99999人", "color": "#FF6666"}, {"min": 10000, "max": 99999, "lable": "10000~99999人", "color": "#CC3333"}, {"min": 100000, "lable": "100000+", "color": "#990033"}, ] ) ) # 绘图 map.render("全国疫情地图.html")
""" 演示河南省疫情地图开发 """ import json from pyecharts.charts import Map from pyecharts.options import * # 读取文件 f = open("E:/疫情.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) # 手动添加济源市的数据 data_list.append(("济源市", 5)) # 构建地图 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": "#CCFFFF"}, {"min": 100, "max": 999, "lable": "100~9999人", "color": "#FFFF99"}, {"min": 1000, "max": 4999, "lable": "1000~4999人", "color": "#FF9966"}, {"min": 5000, "max": 9999, "lable": "5000~99999人", "color": "#FF6666"}, {"min": 10000, "max": 99999, "lable": "10000~99999人", "color": "#CC3333"}, {"min": 100000, "lable": "100000+", "color": "#990033"}, ] ) ) # 绘图 map.render("河南省疫情地图.html")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。