文章目录
- 1.基础地图
- 2.国内疫情地图
- 3.省内疫情地图
1.基础地图
(1)基础结构
from pyecharts.charts import Map
map=Map() # 准备地图对象
data=[ # 数据是由元组组成的列表("北京市",100),("台湾省",99),("上海市",98),("广东省",97)
]
map.add("名称",data,"china") # 添加数据,(名称,数据,地图类型(如中国地图))
map.render() # 生成地图
(2)全局选项-颜色
颜色查找
from pyecharts.options import VisualMapOpts
map.set_global_opts( # 全局选项visualmap_opts=VisualMapOpts( # 可视化地图的相关设置(如设置颜色)is_show=True, # 展示is_piecewise=True, # 开启手动校准范围pieces=[ # 由字典组成的列表{"min":1,"max":9,"label":"1-9人","color":"#B22222"},{"min":10,"max":19,"label":"10-19人","color":"#A52A2A"},{"min":20,"max":29,"label":"20-29人","color":"#E9967A"},{"min":30,"max":39,"label":"30-39人","color":"#FA8072"},{"min":40,"max":49,"label":"40-49人","color":"#FFA07A"},{"min":50,"max":59,"label":"50-59人","color":"#FFA500"},{"min":60,"max":69,"label":"60-69人","color":"#FF8C00"},{"min":70,"max":79,"label":"70-79人","color":"#FF7F50"},{"min":80,"max":89,"label":"80-89人","color":"#F08080"},{"min":90,"max":99,"label":"90-99人","color":"#FF6347"},])
)
完整代码
from pyecharts.charts import Map # 地图
from pyecharts.options import VisualMapOpts # 可视化地图的相关设置(如设置颜色)map=Map() # 准备地图对象
data=[ # 数据是由元组组成的列表("北京市",99),("台湾省",90),("上海市",80),("广东省",70)
]
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":"#B22222"},{"min":10,"max":19,"label":"10-19人","color":"#A52A2A"},{"min":20,"max":29,"label":"20-29人","color":"#E9967A"},{"min":30,"max":39,"label":"30-39人","color":"#FA8072"},{"min":40,"max":49,"label":"40-49人","color":"#FFA07A"},{"min":50,"max":59,"label":"50-59人","color":"#FFA500"},{"min":60,"max":69,"label":"60-69人","color":"#FF8C00"},{"min":70,"max":79,"label":"70-79人","color":"#FF7F50"},{"min":80,"max":89,"label":"80-89人","color":"#F08080"},{"min":90,"max":99,"label":"90-99人","color":"#FF6347"},])
)map.render() # 生成地图
2.国内疫情地图
(1)数据获取
数据下载
(2)前期准备
import json
f = open("D:/地图数据/疫情.txt","r",encoding="UTF-8")
data=f.read()
f.close()
data_dict = json.loads(data) # json转python
(3)数据处理
data_province_list = data_dict["areaTree"][0]["children"]data_list=[] # 用于存储绘图需要用到的数据列表
for data_province in data_province_list: # 遍历各个省份的数据province_name = data_province["name"]province_confirm = data_province["total"]["confirm"]data_list.append((province_name,province_confirm)) # 省份名和确诊人数组成元组存入列表,用于绘图
此时data_list ↓
此处需要对省份名规范处理
if province_name=="内蒙古":province_name="内蒙古自治区"elif province_name=="广西":province_name="广西壮族自治区"elif province_name=="西藏":province_name="西藏自治区"elif province_name=="宁夏":province_name="宁夏回族自治区"elif province_name=="新疆":province_name="新疆维吾尔自治区"elif province_name=="北京" or province_name=="上海" or province_name=="天津" or province_name=="重庆":province_name=province_name+"市"elif province_name=="香港" or province_name=="澳门":province_name=province_name+"特别行政区"else:province_name = province_name + "省"
(4)图像绘制
from pyecharts.charts import Map
map=Map()
map.add("各省份确诊人数",data_list,"china")from pyecharts.options import *
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") # 指定生成文件名,默认render.html
完整代码
import json
f = open("D:/地图数据/疫情.txt","r",encoding="UTF-8")
data=f.read()
f.close()
data_dict = json.loads(data) # json转python
data_province_list = data_dict["areaTree"][0]["children"]data_list=[] # 用于存储绘图需要用到的数据列表
for data_province in data_province_list: # 遍历各个省份的数据province_name = data_province["name"]if province_name=="内蒙古":province_name="内蒙古自治区"elif province_name=="广西":province_name="广西壮族自治区"elif province_name=="西藏":province_name="西藏自治区"elif province_name=="宁夏":province_name="宁夏回族自治区"elif province_name=="新疆":province_name="新疆维吾尔自治区"elif province_name=="北京" or province_name=="上海" or province_name=="天津" or province_name=="重庆":province_name=province_name+"市"elif province_name=="香港" or province_name=="澳门":province_name=province_name+"特别行政区"else:province_name = province_name + "省"province_confirm = data_province["total"]["confirm"]data_list.append((province_name,province_confirm)) # 省份名和确诊人数组成元组存入列表,用于绘图from pyecharts.charts import Map
map=Map()
map.add("各省份确诊人数",data_list,"china")from pyecharts.options import *
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") # 指定生成文件名,默认render.html
3.省内疫情地图
以河南省为例(数据与上面相同)
(1)前期准备
import json
f = open("D:/地图数据/疫情.txt","r",encoding="UTF-8")
data=f.read()
f.close()
data_dict = json.loads(data) # json转python
henan_province_list = data_dict["areaTree"][0]["children"][3]["children"] # 更新
(2)数据处理
data_list=[] # 用于存储绘图需要用到的数据列表
for data_henan in henan_province_list: # 遍历河南各市henan_name = data_henan["name"]if henan_name=="济源示范区":henan_name="济源市"else:henan_name=henan_name+"市"henan_confirm = data_henan["total"]["confirm"]data_list.append((henan_name,henan_confirm))
(3)图像绘制
from pyecharts.charts import Map
map=Map()
map.add("河南省疫情分布",data_list,"河南")from pyecharts.options import *
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:/地图数据/疫情.txt","r",encoding="UTF-8")
data=f.read()
f.close()
data_dict = json.loads(data) # json转python
henan_province_list = data_dict["areaTree"][0]["children"][3]["children"]data_list=[] # 用于存储绘图需要用到的数据列表
for data_henan in henan_province_list: # 遍历河南各市henan_name = data_henan["name"]if henan_name=="济源示范区":henan_name="济源市"else:henan_name=henan_name+"市"henan_confirm = data_henan["total"]["confirm"]data_list.append((henan_name,henan_confirm))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,"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")