Matplotlib 繪制疫情動(dòng)圖:美國(guó)死亡超21萬(wàn)!
本文使用 matplotlib,繪制 COVID-19 過(guò)去半年四個(gè)國(guó)家的每天死亡人數(shù),獲取數(shù)據(jù)的API接口為:
https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv
數(shù)據(jù)處理的邏輯如下,參考前幾天推送的處理邏輯:
df?=?pd.read_csv('a.csv',?delimiter=',',?header='infer')
df_interest?=?df.loc[df['Country/Region'].isin(['United?Kingdom',?'US',?'Italy',?'Germany'])&?df['Province/State'].isna()]
df_interest.rename(index=lambda?x:?df_interest.at[x,?'Country/Region'],?inplace=True)
df1?=?df_interest.transpose()
df1?=?df1.drop(['Province/State',?'Country/Region',?'Lat',?'Long'])
df1?=?df1.loc[(df1?!=?0).any(1)]
df1.index?=?pd.to_datetime(df1.index)
為了更方便大家理解,展示df_interest的部分?jǐn)?shù)據(jù):

整理后df1的部分?jǐn)?shù)據(jù):

可以看到截止昨天,美國(guó)COVID-19死亡人數(shù)已有:219286
繪制折線圖動(dòng)畫(huà)展示的邏輯如下:
color?=?['red',?'green',?'blue',?'orange']
fig?=?plt.figure()
plt.xticks(rotation=45,?ha="right",?rotation_mode="anchor")
plt.subplots_adjust(bottom?=?0.2,?top?=?0.9)
plt.ylabel('No?of?Deaths')
plt.xlabel('Dates')
#?此函數(shù)是繪制動(dòng)畫(huà)的回調(diào)函數(shù)
#?有且僅有一個(gè)參數(shù)?i,表示幀數(shù),表示df1的第幾行
def?showLine(i):
????plt.legend(df1.columns)
????p?=?plt.plot(df1[:i].index,?df1[:i].values)
????for?i?in?range(0,4):
????????p[i].set_color(color[i])
繪制動(dòng)畫(huà)只有這一行,調(diào)用FuncAnimation,它的第二個(gè)參數(shù)為上面定義的函數(shù)showLine:
animator?=?ani.FuncAnimation(fig,?showLine,?interval?=?10)
plt.show()
繪制后的折線圖動(dòng)畫(huà)為:

關(guān)于本文有任何疑問(wèn)歡迎留言或加入討論群,在群里統(tǒng)一發(fā)放COVID-19數(shù)據(jù)文件:
長(zhǎng)按加群討論+數(shù)據(jù)文件發(fā)放
評(píng)論
圖片
表情
