pyecharts practice --2019-nCoV epidemic data visualization map (real-time updates)

2019-nCoV epidemic data visualization map results are as follows:

Here Insert Picture Description

Acquiring data (data obtained from this article Tencent real-time tracking of the epidemic)

Tencent real-time tracking of the epidemic website: Click here .
I am using here is Google, tracking data obtained in the following figure:
Here Insert Picture Description
Double-click the left getOnsInfo name = disease_h5 & callback ······, Google opened a new page, showcase the latest illness? data:
Here Insert Picture Description
data analysis:
Here Insert Picture Description
At this point, we get the data needed it!

We requests the library to collect real-time data; data due to get a json format, so we need to json library to parse the data; linked list data can use the pandas library easily standardized data (of course, if you do not use the pandas still can slightly alter some code can be).

Man of few words said on the code:

# 腾讯的疫情实时追踪接口获取json格式疫情数据
def get_ncp_data():
    url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
    data = requests.get(url).json()['data']
    return data

# 扁平化中国疫情数据
def flatten_ncp_data():
    all = json.loads(get_ncp_data())
    #  初始化结果链表
    provinces = []
    date = all['lastUpdateTime']
    china = all['areaTree'][0]['children']  
    #  获取各省份确诊病例
    for province in china:
        province_ncp = province['children']
        province_ncp = {
            '日期': date,
            '省份': province['name'],
            '累计确认': province['total']['confirm']
        }
        provinces.append(province_ncp)       
    return provinces

data visualization

As used herein, it is pyecharts library (of course, you can also use Matplotlib and others)

#  渲染可视化地图
def render_map_chart():
    provinces = flatten_ncp_data()
    df = pd.DataFrame(provinces)
    
    map_chart = Map()
    map_chart.add(
        "全国NCP确诊病例分布图",
        [list(z) for z in zip(list(df["省份"]), list(df['累计确认']))],
        "china",
        is_map_symbol_show=False
    )

    map_chart.set_global_opts(
        title_opts=opts.TitleOpts(
            title="NCP疫情地图(" + str(datetime.date.today()) + ")"
        ),
        visualmap_opts=opts.VisualMapOpts(
            is_piecewise=True,
            pieces=[
                {"min": 1, "max": 9, "label": "1-9人", "color": "#FFE6BE"},
                {"min": 10, "max": 99, "label": "10-99人", "color": "#FFB769"},
                {"min": 100, "max": 499, "label": "100-499人", "color": "#FF8F66"},
                {"min": 500, "max": 999, "label": "500-999人", "color": "#ED514E"},
                {"min": 1000, "max": 9999, "label": "1000-9999人", "color": "#CA0D11"},
                {"min": 10000, "max": 100000, "label": "10000人以上", "color": "#A52A2A"}
            ]))

    map_chart.render('ncp_map_{}.html'.format(datetime.date.today()))


Well, here it is almost over, is not very easy to use them! Junior partner may want to learn hands-on practice! !

All codes

python Version: python: 3.8 (32-bit)

You need to install the module:

Data Analysis and Processing (If you do not want to use the pandas would not install), it is recommended to download the corresponding, whl file locally pip install. Download the following address:
numpy: numpy
PANDAS: PANDAS

pyecharts data visualization analysis library
pyecharts: pyecharts

Internet link plate attached here: https: //pan.baidu.com/s/1cGHOxlMloDilIIK_cS8wQw
extraction code: ulu0
copy the contents of this open Baidu network disk phone App, the operation more convenient oh

import requests
import json
import pandas as pd
import datetime

from pyecharts.charts import Map
from pyecharts import options as opts

#  腾讯数据接口获取json格式疫情数据
def get_ncp_data():
    url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
    data = requests.get(url).json()['data']
    
    return data

#  扁平化中国疫情数据
def flatten_ncp_data():
    all = json.loads(get_ncp_data())
    #  初始化结果链表
    provinces = []
    date = all['lastUpdateTime']
    #  获取各省份确诊病例
    china = all['areaTree'][0]['children']  # 获得中国数据
    for province in china:
        province_ncp = province['children']
        province_ncp = {
            '日期': date,
            '省份': province['name'],
            '累计确认': province['total']['confirm']
        }
        provinces.append(province_ncp)
             
    return provinces

#  渲染可视化地图
def render_map_chart():
    provinces = flatten_ncp_data()
    df = pd.DataFrame(provinces)
    
    map_chart = Map()
    map_chart.add(
        "全国NCP确诊病例分布图",
        [list(z) for z in zip(list(df["省份"]), list(df['累计确认']))],
        "china",
        is_map_symbol_show=False
    )

    map_chart.set_global_opts(
        title_opts=opts.TitleOpts(
            title="NCP疫情地图(" + str(datetime.date.today()) + ")"
        ),
        visualmap_opts=opts.VisualMapOpts(
            is_piecewise=True,
            pieces=[
                {"min": 1, "max": 9, "label": "1-9人", "color": "#FFE6BE"},
                {"min": 10, "max": 99, "label": "10-99人", "color": "#FFB769"},
                {"min": 100, "max": 499, "label": "100-499人", "color": "#FF8F66"},
                {"min": 500, "max": 999, "label": "500-999人", "color": "#ED514E"},
                {"min": 1000, "max": 9999, "label": "1000-9999人", "color": "#CA0D11"},
                {"min": 10000, "max": 100000, "label": "10000人以上", "color": "#A52A2A"}
            ]))

    map_chart.render('ncp_map_{}.html'.format(datetime.date.today()))


if __name__ == '__main__':
    
    render_map_chart()

Released nine original articles · won praise 5 · Views 343

Guess you like

Origin blog.csdn.net/qq_42964349/article/details/104346693
Recommended