新型冠状肺炎全球排名前15位国家感染人数及动态排序图制作,从1月底到5月初

news/2024/7/3 17:07:36

首先爬取who官网疫情数据,由于动图制作数据太多报错,因此截取其排名前15国家。

效果如下:
在这里插入图片描述
由于文件太大,csdn只支持5兆上传,因此压缩后模糊了

完整代码如下:

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import  matplotlib.ticker as ticker
import matplotlib.animation as animation
import time, datetime
matplotlib.rcParams['animation.embed_limit'] = 2**128

df = pd.read_csv(r'./肺炎数据爬取代码/who肺炎数据.csv',
                 usecols=['Country', 'Region', 'day', 'Cumulative_Confirmed'])

colors = dict(zip(
    ['AFRO', 'AMRO', 'EMRO', 'EURO',
     'SEARO', 'WPRO'],
    ['#adb0ff', '#ffb3ff', '#90d595', '#e48381',
     '#aafbff', '#f7bb5f']
))

group_lk = df.set_index('Country')['Region'].to_dict()
fig, ax = plt.subplots(figsize=(15, 8))

def draw_barchart(day):
    dff = df[df['day'].eq(day)].sort_values(by='Cumulative_Confirmed', ascending=True).tail(15)
    ax.clear()
    ax.barh(dff['Country'], dff['Cumulative_Confirmed'], color=[colors[group_lk[x]] for x in dff['Country']])
    dx = dff['Cumulative_Confirmed'].max() / 200
    for i, (Cumulative_Confirmed, Country) in enumerate(zip(dff['Cumulative_Confirmed'], dff['Country'])):
        ax.text(Cumulative_Confirmed - dx, i, Country, size=14, weight=600, ha='right', va='bottom')
        ax.text(Cumulative_Confirmed - dx, i - .25, group_lk[Country], size=10, color='#444444', ha='right', va='baseline')
        ax.text(Cumulative_Confirmed + dx, i, f'{Cumulative_Confirmed:,.0f}', size=14, ha='left', va='center')
    # ... polished styles
    ax.text(1, 0.4, timestamp_to_format(day).split(' ')[0], transform=ax.transAxes, color='#777777', size=26, ha='right', weight=800)
    ax.text(0, 1.06, 'Population', transform=ax.transAxes, size=12, color='#777777')
    ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}'))
    ax.xaxis.set_ticks_position('top')
    ax.tick_params(axis='x', colors='#777777', labelsize=12)
    ax.set_yticks([])
    ax.margins(0, 0.01)
    ax.grid(which='major', axis='x', linestyle='-')
    ax.set_axisbelow(True)
    ax.text(0, 1.11, 'The Number of COVID-19 infections in the world',
            transform=ax.transAxes, size=24, weight=600, ha='left')
    ax.text(1, 0, 'by YZM', transform=ax.transAxes, ha='right',size=15,
                color='#777777', bbox=dict(facecolor='white', alpha=0.8, edgecolor='white'))
    plt.box(False)


def timestamp_to_format(timestamp=None,format = '%Y-%m-%d %H:%M:%S'):
    if timestamp:
        time_tuple = time.localtime(timestamp)
        res = time.strftime(format,time_tuple)
    else:
        res = time.strftime(format)
    return res


animator = animation.FuncAnimation(fig=fig, func=draw_barchart, frames=range(1579478400, 1588377600,86400))

animator.save("动态排序.gif")

爬取的数据,其excel数据格式如下:
在这里插入图片描述


http://www.niftyadmin.cn/n/2751138.html

相关文章

Pro ASP.NET MVC –第四章 语言特性精华

C#语言有很多特性,并不是所有的程序员都了解本书我们将会使用的C#语言特性。因此,在本章,我们将了解一下作为一个好的MVC程序员需要了解C#语言的特性。 每个特性我们都只是简要介绍。如果你想深入了解LINQ或C#,你可以去参考Adam的…

python 卸载matplotlib_python-Matplotlib:如何从轴上删除一个轮廓元素与其他绘制的元素?...

我正在尝试为高斯混合(高斯混合模型)的均值和协方差的估计动画,我需要在每次迭代时更新均值和协方差的图.这是非常简单的重绘方法,因为我使用的行具有set_data方法,每次更新都可以调用该方法.不幸的是,更新协方差是另一回事,因为轮廓元素表示为QuadContourSet对象,并且没有set_…

开发者论坛一周精粹(第十期):Linux全自动安装wdcp面板脚本

第十期(2017年6月5日-2017年6月9日 ) 自动安装wdcp脚本,支持Centos 很多会员在购买服务器后不会挂载和安装环境,抽空测试了个脚本,提供给像我一样的小白使用,希望你会喜欢! Linux全自动安装wd…

宝莱坞机器人 西瓜_《宝莱坞机器人之恋》电影完整版免费在线观看_2010西瓜影音 - 辛集电影院...

憨包科学家瓦西(拉吉尼坎塔 Rajnikanth 饰)醉心于设计一款智能机器人,加入了大宗精力和情感,模仿本人的外形,教与了机器人各种技艺,带他在身边使他可以尽印度电影宝莱坞机器人之恋剧情介绍(BD中字),故事是这样的&#…

修改了一些模版的CSS

节约界面空间,减少不必要的浪费。恩,CSS是个好东西。

redis队列去重_phpredis提高消息队列的实时性方法(推荐)

数据库存贮都用list形式 要存2个队列 1个用作消息队列保存到数据 还有个 就是用来实时读取数据在redis$redis->lpush($queenkey, json_encode($array));$redis->lpush($listkey, json_encode($array));/*消息队列实例*/public function insertinfo(){$infos array(info1…

本周ASP.NET英文技术文章推荐[02/04 - 02/10]

摘要 本期内容较少,共有5篇文章: UpdateControls:UpdateHistory和AnimatedUpdatePanel 我的《Visual Studio Orcas第一眼印象》演讲稿 Cookie的压缩 为Windows平台优化过的PHP 5.2.1终于发布了 微软公司的数字化冰屋![1] UpdateCo…

Android通过浏览器打开app页面并且传递值

最近公司有个需求,要求从第三方网页端打开一个网页,然后在网页中点击“下载”,“打开”按钮,在app端进行下载和打开操作。这里记录下方法。 首先,网页和app页面进行交互,其实会很快想到JS交互,但…