Display of epidemic situation map (win the battle of epidemic)

We collect the real-time data released by Tencent through the requests library; because the data we get is in json format, we also need the json library to analyze the data; the linked list data can be standardized easily using the pandas Library

import requests
import json
import pandas as pd

#  Tencent data interface obtains json format epidemic data
def get_ncp_data():
    url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
    data = requests.get(url).json()['data']
    return data

#  Flattening China's epidemic data
def flatten_ncp_data():
    all = json.loads(get_ncp_data())
    #  Initialization result list
    cities = []
    #  Latest data update time
    date = all['lastUpdateTime']
    # Level 1: Country
    china = all['areaTree'][0]['children']  # Access to China data
    # Second level: Province
    for province in china:
        province_ncp = province['children']
        # Third floor: City
        for city in province_ncp:
            # Output format
            city_ncp = {
                'date': date,
                'Province': province['name'],
                'city': city['name'],
                'New confirmation': city['today']['confirm'],
                'New cure': city['today']['heal'],
                'New death': city['today']['dead'],
                'Cumulative acknowledgement': city['total']['confirm'],
                'Cumulative cure': city['total']['heal'],
                'Cumulative death': city['total']['dead']
            }
            cities.append(city_ncp)
    return cities

Call the function - you can get the data - and the result is as follows:

data =get_ncp_data()
cities = flatten_ncp_data()
[{'date': '2020-02-13 16:35:45',
  'Province': 'Hubei',
  'city': 'Wuhan',
  'New confirmation': 13436,
  'New cure': 0,
  'New death': 0,
  'Cumulative acknowledgement': 32994,
  'Cumulative cure': 1915,
  'Cumulative death': 1036},
 {'date': '2020-02-13 16:35:45',
  'Province': 'Hubei',
  'city': 'Xiaogan',
  'New confirmation': 123,
  'New cure': 0,
  'New death': 0,
  'Cumulative acknowledgement': 2874,
  'Cumulative cure': 207,
  'Cumulative death': 49},
  .......

Here, use the pyecharts library to draw a real-time epidemic map:

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

#  Rendering visualization map
def render_map_chart():
    cities = flatten_ncp_data()
    df = pd.DataFrame(cities)
    
    map_chart = Map()
    map_chart.add(
        "Whole country NCP Distribution of confirmed cases",
        [list(z) for z in zip(list(df["Province"]), list(df['Cumulative acknowledgement']))],
        "china",
        is_map_symbol_show=False
    )

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

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

render_map_chart()

After calling the function, you can get the following webpage, as shown in the screenshot:

I hope the Chinese war "epidemic" can come to a successful end. I wish everyone good health! The country is prosperous and the people are at peace

29 original articles published, 40 praised, 658 visited
Private letter follow

Tags: JSON

Posted on Thu, 13 Feb 2020 11:18:15 -0500 by pureDesi