今天又重新写了一下,疫情数据直接用百度的数据,只不过这个数据在网页源码里,需要正则查询。
直接把代码贴上。
import re
import requests
import json
url = "https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner"
response = requests.get(url)
# print(response.text)
data_json = re.search(r'script type=\"application\/json\".+?>(.+?)<\/script', response.text)
# data_json.group(1)
type(data_json.group(1))
cov_data = data_json.group(1).encode('utf-8').decode('unicode_escape')
cov_data_json = json.loads(cov_data)
cov_data_json.keys()
component = cov_data_json['component']
len(component)
component[0].keys()
# component[0]['caseList']
caseList = component[0]['caseList']
len(caseList)
confirm = [d['confirmed'] for d in caseList]
area = [d['area'] for d in caseList]
dct_cov = dict(zip(area, confirm))
dct_cov
from pyecharts import options as opts
from pyecharts.charts import Map
c = (
Map()
.add("cov_19", list(dct_cov.items()), "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="中国疫情地图"),
visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True,
pieces =[{"max": 100, "min": 1, "label": "1-9", "color": "#FFDECC"},
{"max": 200, "min": 101, "label": "10-99", "color": "#F1CA95"},
{"max": 500, "min": 201, "label": "100-499", "color": "#E8911D"},
{"max": 800, "min": 501, "label": "500-999", "color": "#DE4617"},
{"max": 1000, "min": 801, "label": "1000-2000", "color": "#DB1500"},
{"max": 10000, "min": 1001, "label": "2000-3000", "color": "#9C0707"},
{"max": 70000, "min": 10000, "label": ">=10000", "color": "#5C020B"}
]),
)
.render("中国疫情地图.html")
)
效果如下图:
华丽的分割线
--------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------
以下是原来的做法,
疫情数据用这个,
点击检查,后台的数据里面有个、
这里有
显示很多天的疫情信息,我们只需要一天的,所以用最后一行。
我这儿是349行。
101行是省份
就这两个。就可以。复制出来。
作图使用pyecharts
这里把fake.province 和fake.values(),替换成我们的就可以了。
但是这样各省人数不一,颜色都是一样的。
所以设置pieces 为、
visualmap_opts=opts.VisualMapOpts(max_=70000, is_piecewise=True,
pieces=[{"max": 100, "min": 1, "label": "1-9", "color": "#00FFFF"},
{"max": 200, "min": 101, "label": "10-99", "color": "#FF69B4"},
{"max": 500, "min": 201, "label": "100-499", "color": "#0000FF"},
{"max": 800, "min": 501, "label": "500-999", "color": "#00BFFF"},
{"max": 1000, "min": 801, "label": "1000-2000", "color": "#228B22"},
{"max": 10000, "min": 1001, "label": "2000-3000", "color": "#FF0000"},
{"max": 70000, "min": 10000, "label": ">=10000", "color": "#FFD700"}
] # 分段 添加图例注释和颜色
结果如下: