Making a real-time data map of the coronavirus in Python
The daily data change of the epidemic affects the hearts of millions of people. How I hope that the news of the epidemic being overcome will come as soon as I wake up.
The first thing I wake up every morning is to look at the changes in the number of confirmed patients. I believe that many of my friends are the same as me. I am a programmer, and I have made such a small program at home, which generates data distribution maps in real-time through Python, which can be the whole country or each province. Technology sharing, we fight the epidemic together.
(The data taken from the website may not be very accurate, mainly for technology sharing, but also forgive me)
National Data Map
Hubei Province
Zhejiang Province
Let’s take a look at the code with everyone!
Here with the help of pyecharts
1.First import the required modules
import requests
import json
import refrom pyecharts.charts
import Mapfrom pyecharts
import options
Import these modules from the command line
Import module
pip3 install requests
pip3 install json
pip3 install re
pip3 install pyecharts
In addition to the above modules need to be imported.
In turn, it is a global map, a provincial map of China, a city map of China, a county map of China, and a map of China’s regions.
Map import as needed
pip3 install echarts-countries-pypkg
pip3 install echarts-china-provinces-pypkg
pip3 install echarts-china-cities-pypkg
pip3 install echarts-china-counties-pypkg
pip3 install echarts-china-misc-pypkg
2. Get data-process data;
#send request, collect data
result= requests.get('https://interface.sina.cn/news/wap/fymap2020_data.d.json?1580097300739&&callback=sinajp_1580097300873005379567841634181')
# handle the data
json_str = re.search("\(+([^)]*)\)+", result.text).group(1)html = f"{json_str}"table = json.loads(f"{html}")
3. Generate maps, explained in detail later;
province_data = []
# loop to read the data for each province
for province in table['data']['list']:
province_data.append((province['name'], province['value']))
city_data = []
#get data for each city
for city in province['city']:
city_data.append((city['mapName'], city['conNum']))
map_province = Map()
map_province.set_global_opts(title_opts=options.TitleOpts(title=province['name'] + "实时疫情图-确诊人数:" + province['value']), v 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"}]))
map_province.add("确诊", city_data, maptype = province['name'])
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"}, {"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"}]))
map_country.add("确诊", province_data, maptype="china")
map_country.render("country.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")
# generate the HTML file
"""
print("complete!!!")
Part of the code explained:
(1)
#Add province data to the list
province_data.append ((province [‘name’], province [‘value’]))
Take out the corresponding value in the data and add it to province_data.
(2)
title_opts = options.TitleOpts (title = “A”)
A is the red framed part of the generated map.
(3)
visualmap_opts = options.VisualMapOpts (is_piecewise = True, # Set whether to display in segments
#Customize the data range and corresponding colors. Here I am using the color value obtained by the color picker, which is not easy.
pieces = [
{“min”: 1000, “label”: ‘> 1000 people’, “color”: “# 6F171F”},
{“min”: 500, “max”: 1000, “label”: ‘500–1000 people’, “color”: “# C92C34”},
{“min”: 100, “max”: 499, “label”: ‘100–499 people’, “color”: “# E35B52”},
{“min”: 10, “max”: 99, “label”: ’10 -99 people ‘, “color”: “# F39E86”},
{“min”: 1, “max”: 9, “label”: ‘1–9 people’, “color”: “# FDEBD0”}]))
is_piecewise = True, # Set whether to display in segments
Then set by pieces.
(4)
#Add the data to generate a map of China, so the maptype must correspond to china.
map_country.add (“Confirmed”, province_data, maptype = “china”)
Add descriptive information and corresponding data to the map, and set the map type at the same time.
(5)
#Everything is done, then generate an html webpage file.
map_country.render (“country.html”)
(6) In this way, a real-time map page of the world, the country, and the province is generated.
Interested friends can try.
If you need source code, you can copy it directly
You can also leave a message
Wish the epidemic passed soon,
Thanks to people from all walks of life.
Your encouragement is my biggest motivation
source from https://mp.weixin.qq.com/s/H2P3PsUtPIRq4EkDA1EaLw
credit to its original author “small streams”