""" Plot overview plot for each country separately """ import matplotlib.pyplot as pp from mpl_toolkits.axisartist.parasite_axes import HostAxes, ParasiteAxes import numpy as np import time as time_module import pickle basename="all_" def plot(data, countries, pop): figsize = (10,5) for loc in data: name = basename+loc time, new_cases, new_deaths, total_cases, total_deaths = data[loc] fig, ax1 = pp.subplots(num=name, figsize=figsize) ax2 = ax1.twinx() ax2.plot(time, np.array(total_deaths)*10, label="Total deaths (x10)", marker="", linestyle="--", color="green") ax2.plot(time, total_cases, label=f"Total cases", marker="", linestyle="-", color="blue") ax1.plot(time, np.array(new_deaths)*10, label="raw new deaths (x10)", color="grey", linestyle=":") ax1.plot(time[3:-3], np.convolve(new_deaths, np.ones((7,))/7, mode="valid")*10, label="new deaths 7day mean (x10)", color="black", linestyle="--", linewidth=2) 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]]) # 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 host1 = HostAxes(ax1, [0.15, 0.1, 0.65, 0.8]) par1 = ParasiteAxes(host1, sharex=host1) host1.append(par1) 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") #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) if loc in pop: pp.title(f"{loc}, population = "+f"{pop[loc]['pop']:,}".replace(",",".")) else: pp.title(loc) 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) pp.savefig("ac_"+name+".png") pp.close(fig)