Daily practice: Python domestic epidemic data crawling and map drawing

[Series Articles]
Python Maps - Using pyecharts to draw world maps, Chinese maps, provincial maps, and municipal maps

renderings

Let's take a look at the renderings first:
insert image description here
you can see that the cumulative confirmed epidemic data in Jilin just and the query are consistent.
insert image description here

Mapping of cumulative confirmed epidemics

① Data capture from time to time

Please refer to the following articles for relevant data on the pneumonia epidemic:

[Related articles]
Daily practice: Python crawler crawling the national new crown pneumonia epidemic data example, using beautifulsoup4 library to achieve

Rendering display of the province: rendering of the
insert image description here
city:
insert image description here

② Obtain provincial epidemic data

Here is the provincial epidemic array data extracted from the json string.

import json

def get_provinces(json_content):
    """
     xiaolanzao, 2022.04.27
    【作用】
     获取省份疫情数据
    【参数】
     json_content : json字符串
    【返回】
     省份累计确诊数组数据
    """
    # 将字符串转化为字典
    json_data = json.loads(json_content)
    
    data = []
    
    # 省份数据展示
    for i in json_data:
        # 省份名称处理,和地图对应
        province_name = i["provinceName"]
        if(len(province_name)>1):
            if(province_name[-1] == "省"):
                province_name = province_name[:-1]
            if(province_name[-1] == "市"):
                province_name = province_name[:-1]
        if(len(province_name)>3):
            if(province_name[-3:] == "自治区"):
                province_name = province_name[:-3]
        if(len(province_name)>3):
            if(province_name[-3:] == "维吾尔"):
                province_name = province_name[:-3]
        if(len(province_name)>2):
            if(province_name[-2:] == "壮族"):
                province_name = province_name[:-2]
            if(province_name[-2:] == "回族"):
                province_name = province_name[:-2]
        
        data.append((province_name, i["confirmedCount"]))
         
    print("全国各省份疫情数据如下:")
    for i in data:
        print(i)
        
    return data


data = get_provinces(json_content)

Note: The original name is processed here, and the province name and the map can only be identified.

# 省份名称处理,和地图对应
province_name = i["provinceName"]
if(len(province_name)>1):
    if(province_name[-1] == "省"):
        province_name = province_name[:-1]
    if(province_name[-1] == "市"):
        province_name = province_name[:-1]
if(len(province_name)>3):
    if(province_name[-3:] == "自治区"):
        province_name = province_name[:-3]
if(len(province_name)>3):
    if(province_name[-3:] == "维吾尔"):
        province_name = province_name[:-3]
if(len(province_name)>2):
    if(province_name[-2:] == "壮族"):
        province_name = province_name[:-2]
    if(province_name[-2:] == "回族"):
        province_name = province_name[:-2]

This is before
insert image description here
processing: This is after processing:
insert image description here

③ Visual configuration item segmentation color data settings

First of all, you need to set the data displayed in a visual configuration item segment, and then it will be displayed as which kind of presentation according to which segment the data is in.

pieces = [
    {
    
    'min': 10000, 'color': '#540d0d'},
    {
    
    'max': 9999, 'min': 1000, 'color': '#9c1414'},
    {
    
    'max': 999, 'min': 500, 'color': '#d92727'},
    {
    
    'max': 499, 'min': 100, 'color': '#ed3232'},
    {
    
    'max': 99, 'min': 10, 'color': '#f27777'},
    {
    
    'max': 9, 'min': 1, 'color': '#f7adad'},
    {
    
    'max': 0, 'color': '#f7e4e4'},
]
# 全局配置项
.set_global_opts(
    # 设置标题
    title_opts=opts.TitleOpts(title="中国疫情地图"),
    # 设置视觉配置项分段显示
    visualmap_opts=opts.VisualMapOpts(
        pieces=pieces,
        is_piecewise=True,
        is_show=True
    )
)

Here is the rendering:
insert image description here

④ Mapping of cumulative confirmed epidemics

Here, a map is generated based on the data.

# -*- coding:utf-8 -*-
# 2022-4-1
# 作者:小蓝枣
# 疫情地图

# 需要引用的库
from pyecharts import options as opts
from pyecharts.charts import Map

pieces = [
    {
    
    'min': 10000, 'color': '#540d0d'},
    {
    
    'max': 9999, 'min': 1000, 'color': '#9c1414'},
    {
    
    'max': 999, 'min': 500, 'color': '#d92727'},
    {
    
    'max': 499, 'min': 100, 'color': '#ed3232'},
    {
    
    'max': 99, 'min': 10, 'color': '#f27777'},
    {
    
    'max': 9, 'min': 1, 'color': '#f7adad'},
    {
    
    'max': 0, 'color': '#f7e4e4'},
]

def create_china_map():
    ''' 
     作用:生成中国疫情地图
    '''
    (
        Map()
        .add(
            series_name="累计确诊", 
            data_pair=data, 
            maptype="china", 
            # 是否默认选中,默认为True
            is_selected=True,
            # 是否启用鼠标滚轮缩放和拖动平移,默认为True
            is_roam=True,
            # 是否显示图形标记,默认为True
            is_map_symbol_show=False
        )
        # 系列配置项
        # 关闭标签名称显示
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True))
        # 全局配置项
        .set_global_opts(
            # 设置标题
            title_opts=opts.TitleOpts(title="中国疫情地图"),
            # 设置视觉配置项分段显示
            visualmap_opts=opts.VisualMapOpts(
                pieces=pieces,
                is_piecewise=True,
                is_show=True
            )
        )
        # 生成本地html文件
        .render("中国疫情地图.html")
    )
    
create_china_map()

The effect diagram is as follows:
insert image description here

Mapping of existing confirmed epidemics

① Obtain provincial epidemic data

The difference between here and the above is data.append((province_name, i["confirmedCount"]))replaced bydata.append((province_name, i["currentConfirmedCount"]))

import json

def get_provinces(json_content):
    """
     xiaolanzao, 2022.04.27
    【作用】
     获取省份疫情数据
    【参数】
     json_content : json字符串
    【返回】
     省份现存确诊数组数据
    """
    # 将字符串转化为字典
    json_data = json.loads(json_content)
    
    data = []
    
    # 省份数据展示
    for i in json_data:
        # 省份名称处理,和地图对应
        province_name = i["provinceName"]
        if(len(province_name)>1):
            if(province_name[-1] == "省"):
                province_name = province_name[:-1]
            if(province_name[-1] == "市"):
                province_name = province_name[:-1]
        if(len(province_name)>3):
            if(province_name[-3:] == "自治区"):
                province_name = province_name[:-3]
        if(len(province_name)>3):
            if(province_name[-3:] == "维吾尔"):
                province_name = province_name[:-3]
        if(len(province_name)>2):
            if(province_name[-2:] == "壮族"):
                province_name = province_name[:-2]
            if(province_name[-2:] == "回族"):
                province_name = province_name[:-2]
        
        data.append((province_name, i["currentConfirmedCount"]))
         
    print("全国各省份疫情数据如下:")
    for i in data:
        print(i)
        
    return data


data = get_provinces(json_content)

② Mapping of existing confirmed epidemics

Here I just changed the dataset name, from series_name="累计确诊"changed toseries_name="现存确诊"

# -*- coding:utf-8 -*-
# 2022-4-1
# 作者:小蓝枣
# 疫情地图

# 需要引用的库
from pyecharts import options as opts
from pyecharts.charts import Map

pieces = [
    {
    
    'min': 10000, 'color': '#540d0d'},
    {
    
    'max': 9999, 'min': 1000, 'color': '#9c1414'},
    {
    
    'max': 999, 'min': 500, 'color': '#d92727'},
    {
    
    'max': 499, 'min': 100, 'color': '#ed3232'},
    {
    
    'max': 99, 'min': 10, 'color': '#f27777'},
    {
    
    'max': 9, 'min': 1, 'color': '#f7adad'},
    {
    
    'max': 0, 'color': '#f7e4e4'},
]

def create_china_map():
    ''' 
     作用:生成中国疫情地图
    '''
    (
        Map()
        .add(
            series_name="现存确诊", 
            data_pair=data, 
            maptype="china", 
            # 是否默认选中,默认为True
            is_selected=True,
            # 是否启用鼠标滚轮缩放和拖动平移,默认为True
            is_roam=True,
            # 是否显示图形标记,默认为True
            is_map_symbol_show=False
        )
        # 系列配置项
        # 关闭标签名称显示
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True))
        # 全局配置项
        .set_global_opts(
            # 设置标题
            title_opts=opts.TitleOpts(title="中国疫情地图"),
            # 设置视觉配置项分段显示
            visualmap_opts=opts.VisualMapOpts(
                pieces=pieces,
                is_piecewise=True,
                is_show=True
            )
        )
        # 生成本地html文件
        .render("中国疫情地图.html")
    )
    
create_china_map()

The renderings are as follows:
insert image description here
if you like it, give it a like ❤!

Guess you like

Origin blog.csdn.net/qq_38161040/article/details/123116983