Last active
July 8, 2020 06:18
-
-
Save nkratzke/dd1bdb1808568455a2018c4ec4e9d21c to your computer and use it in GitHub Desktop.
Generate graphs of relative trends of daily reported COVID19 cases and deaths
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dateparser | |
import pandas as pd | |
import urllib.request | |
# Load data from Our World in Data (OWID) | |
url = "https://covid.ourworldindata.org/data/owid-covid-data.csv" | |
urllib.request.urlretrieve(url, "owid-data.csv") | |
df = pd.read_csv('owid-data.csv') | |
m = 20 | |
countries = df['iso_code'] != 'OWID_WRL' | |
cases = 'new_cases' | |
rcases = 'new_cases_per_million' | |
absolute = df[countries][['iso_code', cases]].groupby(['iso_code']).sum().sort_values(cases, ascending=False).reset_index() | |
relative = df[countries][['iso_code', rcases]].groupby(['iso_code']).sum().sort_values(rcases, ascending=False).reset_index() | |
countries = sorted(set(list(absolute['iso_code'][0:m]) + list(relative['iso_code'][0:m]))) | |
version = max([dateparser.parse(d).date() for d in set(df['date'])]) | |
print(version) | |
# Generate graphs of relative trends of daily reported COVID19 cases and deaths | |
fig, _ = plt.subplots(8, 4, figsize=(30, 30), facecolor='white', sharex=True, sharey=True) | |
n = 7 # 7-days moving average | |
for ax, country in zip(fig.axes, countries): | |
data = df[df['iso_code'] == country][['date', 'new_cases', 'new_deaths']] | |
xs = [dateparser.parse(d) for d in data['date']] | |
for kind in ['new_cases', 'new_deaths']: | |
m = data[kind].rolling(n).mean().max() | |
ys = data[kind].rolling(n).mean() / m | |
ax.plot(xs, ys, label=f"{ kind }", alpha=0.5, linewidth=2) | |
ax.set_title(country) | |
ax.set_facecolor("whitesmoke") | |
ax.set_ylim(0.0, 1.0) | |
ax.xaxis.set_tick_params(rotation=90) | |
ax.spines['top'].set_visible(False) | |
ax.spines['right'].set_visible(False) | |
ax.spines['bottom'].set_visible(False) | |
ax.spines['left'].set_visible(False) | |
ax.legend(frameon=False, facecolor="white") | |
ax.yaxis.grid(True, alpha=0.75) | |
plt.savefig("TOP30-countriy-trends.png") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment