Last active
April 9, 2020 17:08
-
-
Save mdunschen/ba627a271b5e43ef5dea6979b01fcddd to your computer and use it in GitHub Desktop.
This file contains hidden or 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 os, tempfile | |
import pandas | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import datetime | |
import urllib.request as request | |
# load the xlsx file | |
url = "https://fingertips.phe.org.uk/documents/Historic%20COVID-19%20Dashboard%20Data.xlsx" | |
def scrapeLatest(fn = None): | |
if fn == None: | |
fp = tempfile.TemporaryFile() | |
fp.write(request.urlopen(url).read()) | |
return fp | |
else: | |
open(fn, "wb").write(request.urlopen(url).read()) | |
return fn | |
def plotConfirmed(f, places): | |
df = pandas.read_excel(fp, f.sheet_names[5]) | |
dfnp = df.to_numpy() | |
placeData = [] | |
for p in dfnp[8:158]: | |
if type(p[1]) == type('') and p[1].strip() in places: | |
placeData.append(p[1:]) | |
dates = dfnp[6][2:] | |
fig = plt.figure(figsize=(20,10)) | |
for pl in placeData: | |
plt.plot(dates, pl[1:], label=pl[0]) | |
plt.xticks(dates, rotation=45) | |
plt.title("COVID-19 Confirmed Cases") | |
plt.legend() | |
plt.show() | |
def plotDeaths(f): | |
df = pandas.read_excel(fp, f.sheet_names[2]) | |
dfnp = df[6:].to_numpy() | |
np.nan_to_num(dfnp) | |
dates = [d[0] for d in dfnp] | |
fig = plt.figure(figsize=(20,10)) | |
for i in range(1,2): | |
deaths = [d[i] == d[i] and d[i] or 0 for d in dfnp] | |
plt.bar(dates[1:], deaths[1:], label=deaths[0]) | |
plt.xticks(dates[1:], rotation=45) | |
plt.title("Deaths per Day") | |
#plt.legend() | |
plt.show() | |
fig = plt.figure(figsize=(20,10)) | |
for i in range(2,7): | |
deaths = [d[i] == d[i] and d[i] or 0 for d in dfnp] | |
plt.plot(dates[1:], deaths[1:], label=deaths[0]) | |
plt.xticks(dates[1:], rotation=45) | |
plt.title("Cummulative Deaths") | |
plt.legend() | |
plt.show() | |
fn = datetime.datetime.strftime(datetime.datetime.now(), "COVID-19-%Y%d%m.xlsx", ) | |
if (not os.path.exists(fn)): | |
fp = scrapeLatest(fn) | |
else: | |
fp = fn | |
print("Already downloaded") | |
f = pandas.ExcelFile(fp) | |
plotDeaths(f) | |
places = ["Liverpool", "Knowsley", "St. Helens", "Halton", "Sefton", "Wirral"] | |
#places = ["Liverpool", "Birmingham"] | |
plotConfirmed(f, places) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment