地图的显示使用pyechart来实现也十分简单,本文主要进行简单的数据爬取,然后可视化。
数据的来源:腾讯新闻
先确定数据的url:
确认url为:
https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5
导入需要的包:
import json
import requests
from pyecharts import options as opts
from pyecharts.charts import Map
没有的同学情自行下载
从json中获取数据:
url = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"
res = json.loads(requests.get(url).json()["data"])#json.loads()是用来读取字符串
datas = res["areaTree"][0]["children"]
#获取到指定的内容,需要的数据在["areaTree"][0]["children"]里面
#数据预处理方便下面使用
china_total = "确诊:{} 疑似:{} 治愈:{} 死亡:{} 更新日期:{}".format(res["chinaTotal"]["confirm"],
res["chinaTotal"]["suspect"], res["chinaTotal"]["heal"],
res["chinaTotal"]["dead"], res["lastUpdateTime"])
pyechart部分:
for data in datas:
provinces.append(data["name"])
confirm_value.append(data["total"]["confirm"])
cmap = (
Map(init_opts=opts.InitOpts(width="900px", height="700px", page_title="新型冠状病毒疫情地图"))#初始化
.add("确诊", [list(z) for z in zip(provinces, confirm_value)], "china", is_map_symbol_show=False)
.set_series_opts(
label_opts=opts.LabelOpts(is_show=True)
)
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(is_piecewise=True, pieces=[
{"min": 1000, "color": "#70161d"},
{"min": 500, "max": 1000, "color": "#cb2a2f"},
{"min": 100, "max": 500, "color": "#e55a4e"},
{"min": 10, "max": 100, "color": "#f59e83"},
{"min": 1, "max": 10, "color": "#fdebcf"} #设置范围
]),
title_opts=opts.TitleOpts(title="全国新型冠状病毒疫情地图", subtitle=china_total, pos_left="center", pos_top="10px"),
legend_opts=opts.LegendOpts(is_show=False),
tooltip_opts=opts.TooltipOpts(trigger_on="click", formatter='省份:{b}<br/>{a}:{c}')
)
.render("map.html")
)
完整代码如下:
import json
import requests
from pyecharts import options as opts
from pyecharts.charts import Map
url = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"
res = json.loads(requests.get(url).json()["data"])
datas = res["areaTree"][0]["children"]
china_total = "确诊:{} 疑似:{} 治愈:{} 死亡:{} 更新日期:{}".format(res["chinaTotal"]["confirm"],
res["chinaTotal"]["suspect"], res["chinaTotal"]["heal"],
res["chinaTotal"]["dead"], res["lastUpdateTime"])
provinces = []
confirm_value = []
for data in datas:
provinces.append(data["name"])
confirm_value.append(data["total"]["confirm"])
cmap = (
Map(init_opts=opts.InitOpts(width="900px", height="700px", page_title="新型冠状病毒疫情地图"))
.add("确诊", [list(z) for z in zip(provinces, confirm_value)], "china", is_map_symbol_show=False)
.set_series_opts(
label_opts=opts.LabelOpts(is_show=True)
)
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(is_piecewise=True, pieces=[
{"min": 1000, "color": "#70161d"},
{"min": 500, "max": 1000, "color": "#cb2a2f"},
{"min": 100, "max": 500, "color": "#e55a4e"},
{"min": 10, "max": 100, "color": "#f59e83"},
{"min": 1, "max": 10, "color": "#fdebcf"}
]),
title_opts=opts.TitleOpts(title="全国新型冠状病毒疫情地图", subtitle=china_total, pos_left="center", pos_top="10px"),
legend_opts=opts.LegendOpts(is_show=False),
tooltip_opts=opts.TooltipOpts(trigger_on="click", formatter='省份:{b}<br/>{a}:{c}')
)
.render("map.html")
)
结果: