python爬虫疫情可视化

本文通过Python的PyEcharts库展示了如何利用获取的疫情数据,制作一个可交互的中国疫情地图可视化。代码中首先从API获取疫情数据,然后处理数据并用PyEcharts的Map组件进行展示,设置地图类型为'china',并添加视觉映射以突出不同地区的感染人数。最终生成的可视化地图可以在HTML文件中查看,提供了一种直观了解全国疫情情况的方式。
摘要由CSDN通过智能技术生成

学习了pyecharts后来小试牛刀下,来做一个中国疫情可视化

首先安装pyecharts库

1.可以直接在pycharm上装

2.也可以更快命令行装,win+r cmd

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts 

今天要用到的库

from pyecharts import options as opts
from pyecharts.charts import Map

 

数据的话我们可以直接用我们上一篇文章今日疫情数据,想学习之前的代码请参考

python爬虫今日疫情_Lorrey_的博客-CSDN博客 

这里直接用上次代码,不用全要

import requests
import json

url='https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=statisGradeCityDetail,diseaseh5Shelf'
r=requests.post(url)
retext=r.text

data=json.loads(retext)

citylist=data['data']['diseaseh5Shelf']['areaTree'][0]['children']

alist=[]
for i in citylist:
    #print(i['name'],i['total']['nowConfirm'],i['total']['wzz'])
    alist.append([i['name'],i['total']['nowConfirm']+i['total']['wzz']])
print(alist)

 

接下来是pyecharts可视化

Map(init_opts=opts.InitOpts(width="1500px", height="900px", page_title='中国疫情地图'))
    .add(
    '感染人数',
    alist,
    maptype='china',
    is_roam=True,
    is_selected=True,
    is_map_symbol_show=True  # 是否标记图形
)
title_opts=opts.TitleOpts(title="中国加油",
                          title_target="blank",  # 新窗口打开
                          subtitle="中国疫情地图",  # 副标题
                          subtitle_target="self"),  # 当前窗口打开
visualmap_opts=opts.VisualMapOpts(is_show=True,
                                  max_=300000,
                                  is_calculable=True,
                                  is_piecewise=True,
                                  pieces=[
                                            {"min":10000},
                                            {"min":5000,"max":5000},
                                            {"min": 1000, "max": 5000},
                                            {"min": 500, "max": 1000},
                                            {"min": 10, "max": 500},
                                            {"max": 10}
                                        ],
                                  range_text=["High", "Low"],
                                  border_color="#000")

完整代码如下 :

from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.faker import Faker
import requests
import json
from openpyxl import Workbook
from openpyxl import load_workbook

url='https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=statisGradeCityDetail,diseaseh5Shelf'
r=requests.post(url)
retext=r.text

data=json.loads(retext)

citylist=data['data']['diseaseh5Shelf']['areaTree'][0]['children']

alist=[]
for i in citylist:
    #print(i['name'],i['total']['nowConfirm'],i['total']['wzz'])
    alist.append([i['name'],i['total']['nowConfirm']+i['total']['wzz']])
print(alist)

#pyecharts 可视化

provience_yiqing = (
    Map(init_opts=opts.InitOpts(width="1500px", height="900px", page_title='中国疫情地图'))
        .add(
        '感染人数',
        alist,
        maptype='china',
        is_roam=True,
        is_selected=True,
        is_map_symbol_show=True  # 是否标记图形
    )


        .set_global_opts(
        title_opts=opts.TitleOpts(title="中国加油",
                                  title_target="blank",  # 新窗口打开
                                  subtitle="中国疫情地图",  # 副标题
                                  subtitle_target="self"),  # 当前窗口打开

        visualmap_opts=opts.VisualMapOpts(is_show=True,
                                          max_=300000,
                                          is_calculable=True,
                                          is_piecewise=True,
                                          pieces=[
                                                    {"min":10000},
                                                    {"min":5000,"max":5000},
                                                    {"min": 1000, "max": 5000},
                                                    {"min": 500, "max": 1000},
                                                    {"min": 10, "max": 500},
                                                    {"max": 10}
                                                ],
                                          range_text=["High", "Low"],
                                          border_color="#000")

        )
        .set_series_opts(
        label_opts=opts.LabelOpts(is_show=True)
    )
        .render('中国疫情.html')

)

打开html文件,非常美观的可视化

 

  • 14
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lorrey_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值