Files
coronavis/doubling_time.py
2020-12-21 12:04:37 +01:00

40 lines
1.3 KiB
Python

"""
Plot doubling time
"""
import matplotlib.pyplot as pp
import numpy as np
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning)
name="doubling_time"
def moving_average(x, w):
return np.convolve(x, np.ones(w), 'valid') / w
def plot(data, countries, pop, **kwargs):
figsize = (10,5)
for loc in data:
if loc not in countries:
continue
time, new_cases, new_deaths, total_cases, total_deaths, total_vaccinations = data[loc]['time'], data[loc]['new_cases'], data[loc]['new_deaths'], data[loc]['total_cases'], data[loc]['total_deaths'], data[loc]['total_vaccinations']
pp.figure(name, figsize=figsize)
window_size = 7
tchar = moving_average(np.array(new_cases),window_size)/moving_average(np.array(total_cases),window_size)
thalf = np.log(2)/tchar
try:
day_of_100_cases = np.argwhere(np.array(total_cases) > 99)[0][0]
except:
return
new_time_axis = time[int(window_size/2):-int(window_size/2)]
pp.plot(new_time_axis[50:], thalf[50:], label=f"{loc}", marker=".")
pp.yscale("log")
pp.ylabel("doubling time [days]")
#pp.xlabel("daye reported ten cases")
#pp.xticks(rotation=45)
pp.legend(frameon=False)
pp.grid(which="both", axis="y")
pp.tight_layout()
pp.savefig("img/"+name+".png")