师从黑马程序员
基础地图使用
基础地图演示
视觉映射器
具体颜色对应的代码可以在http://www.ab173.com/中查询RGB颜色查询对照表
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,"lable":"1-9","color":"#CCFFFF"},{"min":10,"max":99,"lable":"10-99","color":"#FF6666"},{"min":100,"max":500,"lable":"100-500","color":"#990033"}])
)#绘图
map.render()
国内疫情地图的构建
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: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-9999人","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 ("D:/疫情.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))
#手动添加济源市数据
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-999人","color":"#FFFF99"},{"min":1000,"max":4999,"lable":"1000-4999人","color":"#=FF9966"},{"min":5000,"max":9999,"lable":"5000-9999人","color":"FF6666"},{"min":10000,"max":99999,"lable":"10000-99999人","color":"#CC3333"},{"min":100000,"lable":"100000+","color":"#990033"}])
)#绘图
map.render("河南省疫情地图.html")#设置生成的文件名
若有侵权,请联系作者