导入相应的包获取相应的疫情信息数据分组可视化结果展示完整代码
导入相应的包
代码语言:javascript复制from pyecharts import options as opts
from pyecharts.charts import Map
import requests, json
获取相应的疫情信息
如何爬取信息以及相应信息的含义的讲解可以参看我的另一篇文章《肺炎疫情数据爬取》,变量的定义也保持了一致,这里不再赘述。
代码语言:javascript复制url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
area = requests.get(url).json()
data = json.loads(area['data'])
代码语言:javascript复制# 全球的疫情数量
all_counties = data['areaTree']
数据分组
代码语言:javascript复制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']))
可视化
pyecharts 是一个用于生成 Echarts 图表的类库。Echarts 是百度开源的一个数据可视化 JS 库。个人非常推荐使用pyechats进行可视化。
pyecharts快速入门可以参考这个网站
代码语言:javascript复制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() # 随时随地渲染图表
结果展示
缩放前
缩放后
完整代码
代码语言:javascript复制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()