Crawling novel coronavirus pneumonia real-time data with Python, pyecharts v1.x draw national epidemic heatmap

Operating results (2020-2-13)

HEATMAP
SCATTER
SCATTER partially enlarged Hubei

The basic flow

  1. Grab epidemic data, national data from Tencent epidemic dynamic real-time , directly get to json
  2. Filter out regional geo does not exist (or geo will complain)
  3. geo draw heat map

Data Format

Please refer to the following article in the packet capture analysis of
grab novel coronavirus pneumonia epidemic data with Python, drawing national epidemic distribution

In addition, if the data is text, then press, mainly with regular expressions, one can refer to the following
catch pneumonia novel coronavirus real-time data with Python, draw a map of the city epidemic

Code


#%%
# 全国疫情地区分布(各省确诊病例)
def catch_cn_disease_dis_ex():
    timestamp = '%d'%int(time.time()*1000)
    url_area = ('https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
               '&callback=&_=') + timestamp
    world_data = json.loads(requests.get(url=url_area).json()['data'])
    china_data = jsonpath.jsonpath(world_data, 
                                   expr='$.areaTree[0].children[*]')
    ls_province_names = jsonpath.jsonpath(china_data, expr='$[*].name')
    ls_confirm_vals = jsonpath.jsonpath(china_data, expr='$[*].total.confirm')
    ls_province_confirm = list(zip(ls_province_names, ls_confirm_vals,)) 
    
    ls_city_names = jsonpath.jsonpath(china_data, expr='$[*].children[*].name')
    ls_city_confirm_vals = jsonpath.jsonpath(china_data, expr='$[*].children[*].total.confirm')
    ls_city_confirm = list(zip(ls_city_names, ls_city_confirm_vals))
    return ls_province_confirm, world_data, ls_city_confirm


#%%
g_ls_area_cfm_tuple =  catch_cn_disease_dis_ex()[2]
#%%
import numpy as np
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType

# 过滤掉无效地区,如'地区待确认'、'外地来沪'等
# 注:数据源中台湾部分的数据未细化到城市,城市name为‘地区待确认’
def filter_city_cfm_by_geo(ls_area_cfm_tuple):
    ls_cfm_legal_cities = []
    c = Geo().add_schema(maptype='china')
    for city_name, city_cfm_val in ls_area_cfm_tuple:
        if c.get_coordinate(name=city_name) is None:
            print(city_name)
        else:            
            ls_cfm_legal_cities.append(tuple([city_name, city_cfm_val]))
    print(ls_cfm_legal_cities)
    return ls_cfm_legal_cities
#%%
ls_cfm_legal_cities = filter_city_cfm_by_geo(g_ls_area_cfm_tuple)
print(ls_cfm_legal_cities)
#%%
# 绘制全国疫情热力图
def heatmap_cn_disease_dis(ls_cfm_legal_cities) -> Geo:
    c = (
        Geo(init_opts=opts.InitOpts(width='100%', height='900px'))
        .add_schema(maptype='china')
        .add('中国', ls_cfm_legal_cities, type_=ChartType.HEATMAP) # EFFECT_SCATTER|SCATTER|HEATMAP
       # .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True,        
                                                   # 把name中的经纬度干掉
                                                   formatter=JsCode("""
                                                           function(params){                                                                                                         
                                                                if (typeof(params.data) == 'undefined') {
                                                                    return params.value[2];  
                                                                } else {          
                                                                    return params.data.name + params.data.value[2] + '例' ;
                                                                }                                                                                                                 
                                                           }"""
                                                   ))
                                 ) 
        # 数目差别太大不宜用连续颜色,opts.VisualMapOpts(is_show=True, min_=0, max_=1000),
        .set_global_opts(visualmap_opts=opts.VisualMapOpts(is_show=True,  
                                                           #is_piecewise=False,  # 是否为分段型
                                                           pos_top='center',
                                                           # min_=0,
                                                           # max_=500, # 怎么设置差额巨大数据的颜色分布呢用分段?  
                                                           is_piecewise=True,  # 是否为分段型
                                            
                                                           pieces=[
                                                               {'min': 10000}, 
                                                               {'min': 1000, 'max': 10000},
                                                               {'min': 500, 'max': 999},
                                                               {'min': 100, 'max': 499},
                                                               {'min': 10, 'max': 99},
                                                               {'min': 0, 'max': 5} ],                                              
                                                           ), 
                             title_opts=opts.TitleOpts(title='冠状病毒疫情热力图')
                            
         )
    )
    return c

#%%
heatmap_cn_disease_dis(ls_cfm_legal_cities).render('中国疫情热力地图.html')

Series of articles

First: grab novel coronavirus pneumonia real-time data with Python, based on Basemap draw provincial epidemic distribution
Second: grab novel coronavirus pneumonia epidemic data with Python, drawing national epidemic distribution (Basemap) and History
Part III: grab novel coronavirus pneumonia real-time data with Python, based on Basemap draw a map of the city epidemic
Part IV: crawling novel coronavirus pneumonia real-time data with Python, pyecharts v1.x draw provinces epidemic map

Further, pyecharts case:
with Python pyecharts v1.x drawing pattern (a): histogram, the columnar stacking chart, bar chart, histogram, Pareto chart, pie, donut, roses
with Python pyecharts v1. x draw graphics (II): line, line area, scatter, radar, box plot, the word cloud

Try not to go out, go out wearing masks, united war Yimo!
Hubei God Bless! God Bless China!

Published 49 original articles · won praise 42 · Views 150,000 +

Guess you like

Origin blog.csdn.net/shineych/article/details/104304969