【武汉加油⛽️】基于Pyecharts的疫情数据可视化~

【武汉加油⛽️】基于Pyecharts的疫情数据可视化~_第1张图片

前言

疫情终将过去,静待春暖花开~

本文使用的数据源来自https://lab.isaaclin.cn/nCoV/zh;

运行本项目需要将pyecharts版本更新至v1.6.2,!pip install pyecharts == 1.6.2

⚡️更好的阅读体验访问我的Kesci Lab:点我跳转;

相关教程:

  • ✨Pyecharts教程:超详细Pyecharts 1.x教程,让你的图表动起来~

获取数据

  • ☀️接口地址:https://lab.isaaclin.cn/nCoV/api/area
  • 请求方式:GET
  • ❄️主要字段:
变量名 注释
currentConfirmedCount 现存确诊人数
confirmedCount 累计确诊人数
suspectedCount 疑似感染人数
curedCount 治愈人数
deadCount 死亡人数

代码 & 效果

导库 & 数据请求

import requests
import pyecharts
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
from datetime import date

# 数据来源于 Github DXY-COVID-19-Data, 原作者为 Isaac Lin
url = 'https://lab.isaaclin.cn/nCoV/api/area'
data = requests.get(url).json()

# 生成更新日期
update_date = date.today()

全球疫情地图

oversea_confirm = []
for item in data['results']:
    if item['countryEnglishName']:
        oversea_confirm.append((item['countryEnglishName'].replace('United States of America', 'United States')
                                                          .replace('United Kiongdom', 'United Kingdom'), 
                                item['confirmedCount']))
    
    
_map = (
        Map(init_opts=opts.InitOpts(theme='dark'))
        .add("累计确诊人数", oversea_confirm, "world",is_map_symbol_show=False)
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="新型冠状病毒全球疫情地图",
                                     subtitle="更新日期:{}".format(update_date)),
            legend_opts=opts.LegendOpts(is_show=False),
            visualmap_opts=opts.VisualMapOpts(is_show=True, max_=50, 
                                              is_piecewise=False, 
                                              range_color=['#FFFFE0', '#FFFFE0', '#FFA07A', '#CD5C5C', '#8B0000']),
            graphic_opts=[
                    opts.GraphicGroup(
                        graphic_item=opts.GraphicItem(
                            bounding="raw",
                            right=150,
                            bottom=50,
                            z=100,
                        ),
                        children=[
                            opts.GraphicRect(
                                graphic_item=opts.GraphicItem(
                                    left="center", top="center", z=100
                                ),
                                graphic_shape_opts=opts.GraphicShapeOpts(
                                    width=200, height=50
                                ),
                                graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
                                    fill="rgba(0,0,0,0.3)"
                                ),
                            ),
                            opts.GraphicText(
                                graphic_item=opts.GraphicItem(
                                    left="center", top="center", z=100
                                ),
                                graphic_textstyle_opts=opts.GraphicTextStyleOpts(
                                    text=JsCode("['钻石号邮轮', '累计确诊人数:{}人'].join('\\n')"
                                                .format(dict(oversea_confirm)['Diamond Princess Cruise Ship'])),
                                    font="bold 16px Microsoft YaHei",
                                    graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
                                        fill="#fff"
                                    ),
                                ),
                            ),
                        ],
                    )
                ],
        )
    )

_map.render_notebook()

【武汉加油⛽️】基于Pyecharts的疫情数据可视化~_第2张图片

全国疫情地图

province_data = []
for item in data['results']:
    if item['countryName'] == '中国':
        province_data.append((item['provinceShortName'], item['confirmedCount']))
        
_map = (
        Map(init_opts=opts.InitOpts(theme='dark'))
        .add("累计确诊人数", province_data, "china",is_map_symbol_show=False)
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="新型冠状病毒全国疫情地图",
                                     subtitle="更新日期:{}".format(update_date)),
            legend_opts=opts.LegendOpts(is_show=False),
            visualmap_opts=opts.VisualMapOpts(is_show=True, max_=1000, 
                                              is_piecewise=False, 
                                              range_color=['#FFFFE0', '#FFA07A', '#CD5C5C', '#8B0000'])
        )
)

_map.render_notebook()

【武汉加油⛽️】基于Pyecharts的疫情数据可视化~_第3张图片

湖北省内确诊情况

for item in data['results']:
    if item['provinceShortName'] == '湖北':
        hubei_data = item['cities']
        
bar = (
        Bar(init_opts=opts.InitOpts(theme='dark'))
        .add_xaxis([x['cityName'] for x in hubei_data])
        .add_yaxis("累计确诊人数", [x['confirmedCount'] for x in hubei_data])
        .add_yaxis("当前确诊人数", [x['currentConfirmedCount'] for x in hubei_data])
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="新型冠状病毒湖北省内确诊情况",
                                     subtitle="更新日期:{}".format(update_date)),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)),
            legend_opts=opts.LegendOpts(is_show=True),
            graphic_opts=[
                    opts.GraphicGroup(
                        graphic_item=opts.GraphicItem(
                            bounding="raw",
                            right=300,
                            top=120
                        ),
                        children=[
                            opts.GraphicRect(
                                graphic_item=opts.GraphicItem(
                                    left="center", top="center"
                                ),
                                graphic_shape_opts=opts.GraphicShapeOpts(
                                    width=200, height=60
                                ),
                                graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
                                    fill="rgba(0,0,0,0.3)"
                                ),
                            ),
                            opts.GraphicText(
                                graphic_item=opts.GraphicItem(
                                    left="center", top="center", z=1
                                ),
                                graphic_textstyle_opts=opts.GraphicTextStyleOpts(
                                    text=JsCode("['当前确诊人数:', '','累计确诊人数-死亡人数-治愈人数'].join('\\n')"),
                                    font="bold 12px Microsoft YaHei",
                                    graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(
                                        fill="#fff"
                                    ),
                                ),
                            ),
                        ],
                    )
                ],
                )
        )

bar.render_notebook()

【武汉加油⛽️】基于Pyecharts的疫情数据可视化~_第4张图片

全国确诊热力地图

cities_data = []
for item in data['results']:
    if item['countryName'] == '中国':
        cities_data.extend((item['cities']))
        
geo = (
        Geo(init_opts=opts.InitOpts(theme='dark'))
        .add_schema(maptype="china", zoom=3, center=[114.31,30.52])
        .add("累计确诊人数", 
             [(i['cityName'], i['currentConfirmedCount']) for i in cities_data 
              if i['cityName'] in pyecharts.datasets.COORDINATES.keys()], 
             type_='heatmap',
             symbol_size=3,
             progressive=50)
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="新型冠状病毒全国疫情热力图",
                                     subtitle="更新日期:{}".format(update_date),
                                     pos_left='right'),
            legend_opts=opts.LegendOpts(is_show=False),
            visualmap_opts=opts.VisualMapOpts(is_show=True, 
                                              is_piecewise=False, 
                                              range_color=['blue', 'green', 'yellow', 'yellow', 'red'])
        )
)

geo.render_notebook()


  • Coding不易,欢迎点赞支持~

你可能感兴趣的:(数据可视化)