【转载!感觉数据可视化很有趣】、‘疫情分布实时图’的代码实现。
from time import time
from datetime import datetime
import requests, json
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
#爬取公开数据
def get_data(url_fmt):
url = url_fmt.format(int(time()*1000))
response = requests.get(url)
if response.status_code == 200:
data = json.loads(response.json()['data'])
return data
else:
return None
#数据预处理
def extract_data(url_data):
china = url_data['areaTree'][0] country = china['name']
provice = china['children']
data = []
for d in provice:
pn = d['name']
for item in d['children']:
cache = {'country': country, 'area': pn, 'city': item['name']}
cache.update(item['total'])
data.append(cache)
return pd.DataFrame(data)
Tencentdata = get_data(url_fmt)
全国疫情总览
chinaTotal = Tencentdata['chinaTotal']
print("{:%Y-%m-%d %H:%M:%S} 目前确诊总人数: {}, 疑似人数: {}, 死亡人数: {}, " \
"治愈人数: {}, 死亡率: {:.2f}%".format(
datetime.now(),
chinaTotal['confirm'],
chinaTotal['suspect'],
chinaTotal['dead'],
chinaTotal['heal'],
int(chinaTotal['dead']) / int(chinaTotal['confirm']) * 100))
## 感觉这个{} 用法值得记忆
data = extract_data(Tencentdata)
data
dt = data.groupby('area').sum()
title = '感染人数前十省份情况'
dt.sort_values(by='confirm', ascending=False)[:10].plot(kind='barh', figsize=(12, 7), title=title)
plt.show()
from pyecharts.charts import Map
from pyecharts import options as op
provice, values = dt.index.to_list(), dt.confirm.to_list()
map_data = [[p, v] for p, v in zip(provice, values)]
map_data1 = [[p, v] for p, v in zip(provice, dt.suspect.to_list())]
# 疫情分布地图
_map = Map()
_map.set_global_opts(title_opts=opts.TitleOpts(title="nCov省份分布!"),
visualmap_opts=opts.VisualMapOpts(max_=1000, ))
_map.add('确诊人数', map_data, maptype='china', is_map_symbol_show=False)
_map.add('疑似人数', map_data1, maptype='china', is_map_symbol_show=False)
_map.render('map.html')
tx数据:url_fmt = r'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_={}