(Source code) Use Python to make real-time data maps of the epidemic (PS: the whole country and each province)

[Special statement: Reprinted from Knowing a Daniel, only slightly modified, if infringement of rights, please contact to delete]

The daily data changes of the epidemic affect the hearts of thousands of people. How I hope that the good news that the epidemic has been overcome will come as soon as I wake up in the morning. The first thing I wake up every morning is to look at the changes in the number of confirmed cases. I believe many friends are the same as me. I am a programmer and I made such a small program at home to generate a data distribution map in real time through Python, which can be across the country or in each province. Technology sharing, we defeat the epidemic together.

(The data taken from the website is not necessarily very accurate, mainly for technology sharing, but I hope to understand)

National Data Map
Insert picture description here

Hubei Province
Insert picture description here

Let's take a look at the code with everyone!

Here with the help of pyecharts

1. Import the required modules first


```python
#如果没有这些模块记得先导入
import requests
import json
import re
from pyecharts.charts import Map
from pyecharts import options

Import these modules on the command line

Import module

pip install requests

pip install json

pip install re

pip install pyecharts

In addition to the above modules, you also need to import the map

Followed by the global map, China provincial map, China city map, China district and county map, China regional map

Map import as needed

pip install echarts-countries-pypkg

pip install echarts-china-provinces-pypkg

pip install echarts-china-cities-pypkg

pip install echarts-china-counties-pypkg

pip install echarts-china-misc-pypkg

Special note : If the download is overtime, please use the domestic mirror to download, the speed is fast

2. Obtain data-process data;

#发起网络请求,获取数据
result = requests.get('https://interface.sina.cn/news/wap/fymap2020_data.d.json?1580097300739&&callback=sinajp_1580097300873005379567841634181')
#使用正则表达式处理数据
json_str = re.search("\(+([^)]*)\)+", result.text).group(1)
html = f"{json_str}"
table = json.loads(f"{html}")

3. Carry out map generation, detailed explanation is in the back;

province_data = []

#循环获取省份名称和对应的确诊数据
for province in table['data']['list']:
    #将省份数据添加到列表中去
    province_data.append((province['name'], province['value']))

    city_data = []
    #循环获取城市名称和对应的确诊数据
    for city in province['city']:
        #这里要注意对应上地图的名字需要使用mapName这个字段
        city_data.append((city['mapName'], city['conNum']))
    #使用Map,创建省份地图    
    map_province = Map()
    #设置地图上的标题和数据标记,添加省份和确诊人数
    map_province.set_global_opts(title_opts=options.TitleOpts(title=province['name'] + "实时疫情图-确诊人数:" + province['value']),
                                 visualmap_opts=options.VisualMapOpts(is_piecewise=True,#设置是否为分段显示
                #自定义数据范围和对应的颜色
                pieces=[
                    {
    
    "min": 1000, "label": '>1000人', "color": "#6F171F"}, 
                    {
    
    "min": 500, "max": 1000, "label": '500-1000人', "color": "#C92C34"},
                    {
    
    "min": 100, "max": 499, "label": '100-499人', "color": "#E35B52"},
                    {
    
    "min": 10, "max": 99, "label": '10-99人', "color": "#F39E86"},
                    {
    
    "min": 1, "max": 9, "label": '1-9人', "color": "#FDEBD0"}]))
    #将数据添加进去,生成省份地图,所以maptype要对应省份。
    map_province.add("确诊", city_data, maptype = province['name'])
    #一切完成,那么生成一个省份的html网页文件,取上对应省份的名字。
    map_province.render(province['name'] + "疫情地图.html")  

#创建国家地图     
map_country = Map()
#设置地图上的标题和数据标记,添加确诊人数
map_country.set_global_opts(title_opts=options.TitleOpts(title="中国实时疫情图-确诊人数:" + table['data']["gntotal"]), 
                            visualmap_opts=options.VisualMapOpts(is_piecewise=True,#设置是否为分段显示
                #自定义数据范围和对应的颜色
                pieces=[
                    {
    
    "min": 1000, "label": '>1000人', "color": "#6F171F"}, # 不指定 max,表示 max 为无限大(Infinity)。
                    {
    
    "min": 500, "max": 1000, "label": '500-1000人', "color": "#C92C34"},
                    {
    
    "min": 100, "max": 499, "label": '100-499人', "color": "#E35B52"},
                    {
    
    "min": 10, "max": 99, "label": '10-99人', "color": "#F39E86"},
                    {
    
    "min": 1, "max": 9, "label": '1-9人', "color": "#FDEBD0"}]))
#将数据添加进去,生成中国地图,所以maptype要对应china。
map_country.add("确诊", province_data, maptype="china")
#一切完成,那么生成一个html网页文件。
map_country.render("中国疫情地图.html")
"""
#world地图,没有详细去完善了,有兴趣的可以试试。

data=[]

for country in table['data']['worldlist']:

    data.append((country['name'], country['value']))
print(data)

map_country =  Map()
map_country.set_global_opts(title_opts=options.TitleOpts(title="世界实时疫情图"), visualmap_opts=options.VisualMapOpts(max_=1000))
map_country.add("确诊", data, maptype="world")
map_country.render("world.html")  # 生成html文件

"""
print("生成完成!!!")

Part of the code explanation :

(1)

#Add province data to the list

province_data.append((province[‘name’], province[‘value’]))

Take the corresponding value in the data and add it to province_data.

(2)

title_opts=options.TitleOpts(title=“A”)

A is the red frame part of the generated map.
Insert picture description here
(3)

visualmap_opts=options.VisualMapOpts(is_piecewise=True,#Set whether to display in segments

#Customize the data range and the corresponding color, here I am the color value obtained by the color picker, it is not easy.

pieces=[

{“min”: 1000, “label”: ‘>1000人’, “color”: “#6F171F”},

{“min”: 500, “max”: 1000, “label”: ‘500-1000人’, “color”: “#C92C34”},

{“min”: 100, “max”: 499, “label”: ‘100-499人’, “color”: “#E35B52”},

{“min”: 10, “max”: 99, “label”: ‘10-99人’, “color”: “#F39E86”},

{“min”: 1, “max”: 9, “label”: ‘1-9人’, “color”: “#FDEBD0”}]))

is_piecewise=True,#Set whether to display in segments

Then set through pieces.
Insert picture description here
(4)

#Add data to generate a map of China, so the maptype must correspond to China.

map_country.add(“确诊”, province_data, maptype=“china”)

Add the description information and corresponding data to the map, and set the map type at the same time.
Insert picture description here
(5)

#Everything is complete, then generate an html web page file.

map_country.render(“country.html”)

(6) In this way, a real-time map page of the world, the country, and the provinces is generated.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45606831/article/details/105129216