新型冠狀肺炎全球排名前15位國家感染人數及動態排序圖製作,從1月底到5月初

首先爬取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數據格式如下:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章