import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.animation as animation
from IPython.display import HTML
df = pd.read_csv('https://gist.githubusercontent.com/ravic499/73aee576217f7386d38212de3058a4a6/raw/278cd260c2372e56ca1509d5193fbbc5fedd7016/covid19.csv',
usecols=['Country','Date','Confirmed'])
df.head(5)
Date | Country | Confirmed | |
---|---|---|---|
0 | 2020-01-22 | Afghanistan | 0 |
1 | 2020-01-22 | Albania | 0 |
2 | 2020-01-22 | Algeria | 0 |
3 | 2020-01-22 | Andorra | 0 |
4 | 2020-01-22 | Angola | 0 |
current_day = "2020-05-23"
dff = (df[df['Date'].eq(current_day)].sort_values(by='Confirmed').head(5))
dff
Date | Country | Confirmed | |
---|---|---|---|
23034 | 2020-05-23 | Lesotho | 2 |
23120 | 2020-05-23 | Western Sahara | 6 |
23068 | 2020-05-23 | Papua New Guinea | 8 |
23040 | 2020-05-23 | MS Zaandam | 9 |
23097 | 2020-05-23 | Suriname | 11 |
fig, ax = plt.subplots(figsize=(15, 8))
ax.barh(dff['Country'], dff['Confirmed'])
<BarContainer object of 5 artists>
fig, ax = plt.subplots(figsize=(15, 8))
ax.barh(dff['Country'], dff['Confirmed'])
for i, (value, name) in enumerate(zip(dff['Confirmed'], dff['Country'])):
ax.text(value, i, name, ha='right')
ax.text(value, i, name, ha='right')
ax.text(value, i, value, ha='left')
ax.text(1, 0.4, current_day, transform=ax.transAxes, size=46, ha='right')
Text(1, 0.4, '2020-05-23')
fig, ax = plt.subplots(figsize=(15, 8))
def draw_barchart(year):
dff = df[df['Date'].eq(year)].sort_values(by='Confirmed').tail(10)
ax.clear()
ax.barh(dff['Country'], dff['Confirmed'])
dx = dff['Confirmed'].max() / 200
for i, (value, name) in enumerate(zip(dff['Confirmed'], dff['Country'])):
ax.text(value-dx, i, name, size=14, weight=600, ha='right', va='bottom')
ax.text(value+dx, i, f'{value:,.0f}', size=14, ha='left', va='center')
ax.text(1, 0.4, year, transform=ax.transAxes, color='#777777', size=46, ha='right', weight=800)
ax.text(0, 1.06, 'Confirmed Cases (thousands)', 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.12, 'The most affected countries in the world as on 2020-05-23',
transform=ax.transAxes, size=24, weight=600, ha='left')
ax.text(1, 0, 'by @ravic499; credit @pratapvardhan', transform=ax.transAxes, ha='right',
color='#777777', bbox=dict(facecolor='white', alpha=0.8, edgecolor='white'))
plt.box(False)
draw_barchart("2020-05-23")
import matplotlib.animation as animation
from IPython.display import HTML
import datetime
date_list=[]
for i in range(0, 130):
date_list.append((datetime.date.today() - datetime.timedelta(i)).isoformat())
date_list=date_list[::-1]
fig, ax = plt.subplots(figsize=(15, 8))
animator = animation.FuncAnimation(fig, draw_barchart, frames=date_list)
HTML(animator.to_jshtml())