From fedae995535642896fd1378bfe0ef4ebdd643c24 Mon Sep 17 00:00:00 2001 From: fordprefect Date: Thu, 2 Apr 2020 13:28:13 +0200 Subject: [PATCH] new plot: doubling time --- coronavis.py | 2 +- doubling_time.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 doubling_time.py diff --git a/coronavis.py b/coronavis.py index 186494d..9cd1027 100644 --- a/coronavis.py +++ b/coronavis.py @@ -14,7 +14,7 @@ sys.path.append(".") # countries of interest countries = ["Germany", "Italy", "Spain", "China", "Japan", "South Korea", "Iran", "United States", "World"] # enabled plots -plots = ["basics", "death_per_case", "normalized_to_first_death", "delay_from_china", "delay_from_usa", "normalized_to_ten_cases", "percent_increase"] +plots = ["basics", "death_per_case", "normalized_to_first_death", "delay_from_china", "delay_from_usa", "normalized_to_ten_cases", "percent_increase", "doubling_time"] ### def get_data(): """fetch data from remote, cache locally and reorganize internal data diff --git a/doubling_time.py b/doubling_time.py new file mode 100644 index 0000000..3478446 --- /dev/null +++ b/doubling_time.py @@ -0,0 +1,34 @@ +""" +Plot doubling time +""" +import matplotlib.pyplot as pp +import numpy as np +name="doubling_time" + +def moving_average(x, w): + return np.convolve(x, np.ones(w), 'valid') / w + +def plot(data, countries): + figsize = (10,5) + for loc in data: + if loc not in countries: + continue + time, new_cases, new_deaths, total_cases, total_deaths = data[loc] + + pp.figure(name, figsize=figsize) + window_size = 3 + tchar = moving_average(np.array(new_cases),window_size)/moving_average(np.array(total_cases),window_size) + thalf = np.log(2)/tchar + day_of_100_cases = np.argwhere(np.array(total_cases) > 99)[0][0] + 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(name+".png")