From 8af2d915cb14ee06b08d175f19f865455303cd0e Mon Sep 17 00:00:00 2001 From: fordprefect Date: Mon, 30 Mar 2020 12:44:35 +0200 Subject: [PATCH] percent increase plot added --- coronavis.py | 2 +- percent_increase.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 percent_increase.py diff --git a/coronavis.py b/coronavis.py index f89af01..186494d 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"] +plots = ["basics", "death_per_case", "normalized_to_first_death", "delay_from_china", "delay_from_usa", "normalized_to_ten_cases", "percent_increase"] ### def get_data(): """fetch data from remote, cache locally and reorganize internal data diff --git a/percent_increase.py b/percent_increase.py new file mode 100644 index 0000000..c416644 --- /dev/null +++ b/percent_increase.py @@ -0,0 +1,32 @@ +""" +Plot percentual increase +""" +import matplotlib.pyplot as pp +import numpy as np +name="percent_increase" + +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] + + # moving average of relative new cases with a window size of 3 days + window_size = 3 + increase = moving_average(np.array(new_cases)/np.array(total_cases), window_size) * 100 + pp.figure(name, figsize=figsize) + delay = 50 # days + pp.plot(time[int(window_size/2)+delay:-int(window_size/2)], increase[delay:], label=f"{loc}", marker=".") + + pp.ylabel("percent increase wrt to previous day") + pp.xticks(rotation=45) + pp.legend(frameon=False) + pp.tight_layout() + + pp.savefig(name+".png") + pp.yscale("log") + pp.savefig(name+"log.png")