From 25923779f7d615fc334f6aeb43034ecea9d8de03 Mon Sep 17 00:00:00 2001 From: fordprefect Date: Tue, 21 Jul 2020 13:16:47 +0200 Subject: [PATCH] manually add some more countries population --- all_countries.py | 42 ++++++++++++++++++++++++++++++++++-------- coronavis.py | 5 ++++- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/all_countries.py b/all_countries.py index eda7ab5..4e2b81a 100644 --- a/all_countries.py +++ b/all_countries.py @@ -27,12 +27,38 @@ def plot(data, countries, pop): ax1.plot(time, new_cases, label="raw new cases", color="grey", linestyle="-") ax1.plot(time[3:-3], np.convolve(new_cases, np.ones((7,))/7, mode="valid"), label="new cases 7day mean", color="orange", linestyle="-", linewidth=2) - # fix lower bound of plot for ax in (ax1, ax2): axis = ax.axis() ax.axis([axis[0], axis[1], -1, axis[3]]) + # if we know population: plot 500 new cases / 1million inhabitants as a rough measure for comparison + # also set color for infection level indicator + infection_level_indicator = "grey" + try: + #if False: + warn_thresh = 500e-6 * pop[loc]['pop'] + info_thresh = 50e-6 * pop[loc]['pop'] + low_thresh = 5e-6 * pop[loc]['pop'] + actual_level = np.mean(new_cases[-7:]) + infection_level_indicator = "green" + if actual_level > low_thresh: + infection_level_indicator = "gold" + if actual_level > info_thresh: + infection_level_indicator = "peru" + if actual_level > warn_thresh: + infection_level_indicator = "r" + bounds = ax1.axis() + #if warn_thresh < bounds[3]: + ax1.plot([bounds[0], bounds[1]], [warn_thresh]*2, color="red", linestyle=":", label=f"500 new cases per 1M inhabitants: {int(warn_thresh):,}".replace(",", ".")) + ax1.plot([bounds[0], bounds[1]], [info_thresh]*2, color="peru", linestyle=":", label=f"50 new cases per 1M inhabitants: {int(info_thresh):,}".replace(",", ".")) + ax1.plot([bounds[0], bounds[1]], [low_thresh]*2, color="gold", linestyle=":", label=f"5 new cases per 1M inhabitants: {int(low_thresh):,}".replace(",", ".")) + + ax1.axis(bounds) + except: + print(f"=====> population unknown for {loc}, skipping plot enhancements") + + # disabled for now: put second total-cases-per-million-inhabitants axis besides total-cases-axis if loc in pop and False: # according to https://matplotlib.org/3.2.2/gallery/axisartist/demo_parasite_axes.html @@ -42,23 +68,23 @@ def plot(data, countries, pop): host2 = HostAxes(ax2, [0.15, 0.1, 0.65, 0.8]) par2 = ParasiteAxes(host2, sharex=host1) host2.append(par2) - - - - ax3 = ax1.twinx() ax3.plot(time, total_cases*1e6/pop[loc]['pop'], linestyle="", marker="") ax3.set_ylabel("total cases per 1M inhabitants") + # plot infection level indicator + ax1.annotate('Infection status:', xy=(0.85, 1.02), xycoords="axes fraction") + ax1.annotate('•', xy=(0.99, 1.02), xycoords="axes fraction", color=infection_level_indicator, fontsize="x-large") #ax1.xticks(rotation=45) #ax1.set_xlabel("date") ax1.set_ylabel("new cases") ax2.set_ylabel("total cases") fig.legend(frameon=False, loc="upper left", bbox_to_anchor=(0,1), bbox_transform=ax1.transAxes) + title = loc if loc in pop: - pp.title(f"{loc}, population = "+f"{pop[loc]['pop']:,}".replace(",",".")) - else: - pp.title(loc) + #pp.title(f"{loc}", population = "+f"{pop[loc]['pop']:,}".replace(",",".")) + title += ", population = "+f"{pop[loc]['pop']:,}".replace(",",".") + ax1.set_title(title) fig.tight_layout() pp.text(0.002,0.005, f"plot generated {time_module.strftime('%Y-%m-%d %H:%M')}, CC-by-sa-nc, origin: dukun.de/corona, datasource: ourworldindata.org/coronavirus-source-data", color="dimgrey", fontsize=8, transform=fig.transFigure) diff --git a/coronavis.py b/coronavis.py index 40ace56..ffb1264 100644 --- a/coronavis.py +++ b/coronavis.py @@ -197,7 +197,10 @@ pop = {'Afghanistan': {'wd_key': 'Afghanistan', 'pop': 34940837}, 'Vietnam': {'wd_key': 'Vietnam', 'pop': 94660000}, 'Yemen': {'wd_key': 'Yemen', 'pop': 28250420}, 'Zambia': {'wd_key': 'Zambia', 'pop': 17094130}, - 'Zimbabwe': {'wd_key': 'Zimbabwe', 'pop': 16529904} + 'Zimbabwe': {'wd_key': 'Zimbabwe', 'pop': 16529904}, + 'World': {'wd_key': 'entered by hand', 'pop': 7713468000}, + 'Taiwan': {'wd_key': 'entered by hand', 'pop': 23574274}, + 'Palestine': {'wd_key': 'entered by hand', 'pop': 4976684}, } ###