Article Directory
Introducing respective packet
from pyecharts import options as opts
from pyecharts.charts import Map
import requests, json
Obtain the appropriate information epidemic
How to explain the meaning of crawling information and corresponding information can refer to my other article "Data crawling pneumonia epidemic" , the definition of variables remain the same, not repeat them here.
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
area = requests.get(url).json()
data = json.loads(area['data'])
# 全球的疫情数量
all_counties = data['areaTree']
Data packets
list = []
all_provinces = all_counties[0]['children']
for i in range(len(all_provinces)):
city_name = all_provinces[i]
list.append((city_name['name'],city_name['total']['confirm']))
Visualization
pyecharts library is used to generate a graph Echarts. Echarts Baidu is a open source data visualization JS library. Personally recommended pyechats for visualization.
pyecharts Quick Start can refer to this website
c = (
Map()
.add(" ",list,"china")
.set_global_opts(title_opts = opts.TitleOpts(title = "中国肺炎确诊分布图"),
visualmap_opts=opts.VisualMapOpts(
is_piecewise=True, # 设置为分段
pieces=[
{"max":9, "min":1, "label": "1-9人"},
{"max":99, "min":10, "label": "10-99人"},
{"max":499, "min":100, "label": "100-499人"},
{"max":999, "min":500, "label": "500-999人"},
{"max":9999, "min":1000, "label": "1000-9999人"},
{"max":99999, "min":10000, "label": "10000人以上"},
])
)
)
# c.render('map.html')
c.render_notebook() # 随时随地渲染图表
The results show
The complete code
from pyecharts import options as opts
from pyecharts.charts import Map
import requests, json
def get_data():
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
area = requests.get(url).json()
data = json.loads(area['data'])
all_counties = data['areaTree']
list = []
all_provinces = all_counties[0]['children']
for i in range(len(all_provinces)):
city_name = all_provinces[i]
list.append((city_name['name'],city_name['total']['confirm']))
def visualize():
c = (
Map()
.add(" ",list,"china")
.set_global_opts(title_opts = opts.TitleOpts(title = "中国肺炎确诊分布图"),
visualmap_opts=opts.VisualMapOpts(
is_piecewise=True, # 设置为分段
pieces=[
{"max":9, "min":1, "label": "1-9人"},
{"max":99, "min":10, "label": "10-99人"},
{"max":499, "min":100, "label": "100-499人"},
{"max":999, "min":500, "label": "500-999人"},
{"max":9999, "min":1000, "label": "1000-9999人"},
{"max":99999, "min":10000, "label": "10000人以上"},
])
)
)
c.render('map.html')
def main():
get_data()
visualize()
if __name__ == '__main__':
main()