赞
踩
目录
导入map地图对象
from pyecharts.charts import Map map = Map()
写入数据
data = [ ("北京市",100), ("上海市",152), ("广东省",102), ("河南省",153), ("湖北省",199), ("台湾省",123) ]
添加数据
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"} ] ) )
颜色表格:RGB颜色对照表-RGB颜色查询对照表-颜色代码表-颜色的英文名称大全-懒人工具|www.ab173.com
绘制地图
map.render()
运行效果
这里已经提前准备好了(JSON数据),如果有需要的可以私我(或者到黑马中找资料):黑马程序员-解锁你的IT职业薪未来! (itheima.com)
首先对JSON数据进行可视化
JSON在线视图查看器(Online JSON Viewer) (ab173.com)
我们要得到各个省份的名称,就要分析数据中省份所处在的层次,如图所示:
# 将JSON数据转换为Python字典 data_dict = json.loads(data) # 从字典中取出省份 province_data_list = data_dict['areaTree'][0]['children'].同理我们也可以获得确诊人数
province_confirm = province_data['total']['confirm'] # 省份确诊人数
获得省份名称
获得确诊人数
对特殊省份的名字处理
因为我们JSON的数据给的时候只给了身份简称,导致地图识别省份时无法匹配,因此我们要自己处理。
for province_data in province_data_list: if province_data['name'] =="北京" or province_data['name'] =="上海" or province_data['name'] =="重庆": province_name = province_data['name']+"市" elif province_data['name'] == "广西": province_name = province_data['name'] + "壮族自治区" elif province_data['name'] == "内蒙古" or 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'] + "回族自治区" elif province_data['name'] == "香港" or province_data['name'] == "澳门": province_name = province_data['name'] + "特别行政区" else: province_name = province_data['name']+"省" # 省份名称 province_confirm = province_data['total']['confirm'] # 省份确诊人数 data_list.append((province_name,province_confirm)) # 将数据添加到列表中
全部代码
- 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_data_list = data_dict['areaTree'][0]['children']
- # 创建一个数据列表存放地图所用的数据
- data_list = []
- # 组装每个省份和确诊人数为元组,并将各个省份的数据封装进入列表内
- for province_data in province_data_list:
- if province_data['name'] =="北京" or province_data['name'] =="上海" or province_data['name'] =="重庆":
- province_name = province_data['name']+"市"
- elif province_data['name'] == "广西":
- province_name = province_data['name'] + "壮族自治区"
- elif province_data['name'] == "内蒙古" or 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'] + "回族自治区"
- elif province_data['name'] == "香港" or province_data['name'] == "澳门":
- province_name = province_data['name'] + "特别行政区"
- else:
- 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="全国疫情地图"),
- 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~999人","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")
运行效果
前面我们准备的数据中包含了各个省份的信息我们取出来使用即可。
对文件进行处理
将JSON数据转换为Python字典
将数据变为元组存放再列表中
构建地图配置全局选项
全部代码
- 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)
- # 取到湖北省的数据
- city_data = data_dict['areaTree'][0]['children'][6]['children']
-
- # 准备数据为元组存放到list中
- data_list = []
- for citydata in city_data:
- if citydata["name"] == "神农架":
- city_name = citydata["name"] + "林区"
- elif citydata["name"] == "恩施州":
- city_name = citydata["name"][:-1]+ "土家族苗族自治州"
- else:
- city_name = citydata["name"] + "市"
- city_confirms = citydata['total']['confirm']
- data_list.append((city_name,city_confirms))
-
-
- # 构建地图
- 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~999人","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 版权所有,并保留所有权利。