diff --git a/dashboard_template.xhtml b/dashboard_template.xhtml index 35a6eb7..5256a41 100644 --- a/dashboard_template.xhtml +++ b/dashboard_template.xhtml @@ -175,16 +175,16 @@

Plots

{% for fig in figures %}
- - {{ fig['caption'] }} - + + + {{ fig['caption'] }} +
Abbildung {{ fig['index'] }}: {{ fig['caption'] }}
Download als PNG Download als PDF + Download als SVG
{% endfor %} diff --git a/plot.py b/plot.py index d47c463..3c5cf69 100644 --- a/plot.py +++ b/plot.py @@ -37,7 +37,8 @@ print_today = today.isoformat() filename_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S") -force_renew = True +force_renew_plots = True +force_renew_dashboard = True # https://www.tagesschau.de/ausland/europa/ursula-von-der-leyen-zu-corona-impfstoffen-101.html target_date_for_herd_immunity = datetime.date(2021, 9, 22) @@ -285,15 +286,47 @@ if os.path.isdir(archive_folder): else: os.mkdir(archive_folder) +def check_recreate_plot(plot_name): + + archive_plot_filename = '{}/{}'.format(archive_folder, plot_name) + + if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew_plots: + print('Plot {} already exists'.format(plot_name)) + return False + + return True + +def save_plot(plot_name): + + folders = [archive_folder, site_folder] + file_formats = ['pdf', 'png', 'svg'] + file_template = '{folder}/{plot_name}.{format}' + + for folder in folders: + for format in file_formats: + plt.savefig(file_template.format(folder=folder, plot_name=plot_name, format=format)) + + print('Created plot {} as {}'.format(plot_name, file_formats)) + +def labeled_timeperiod(ax, start, end, text, color='lightgrey'): + centre = start + (end - start) / 2 + ax.axvspan(start, end, color=color, alpha=0.5) + ax.text(centre, ax.get_ylim()[1], text, bbox={ + 'boxstyle': 'square', + 'fc': color, + 'ec': 'black' + }, ha='center') + + +def add_annotations(ax): + labeled_timeperiod(ax, datetime.date(2021, 3, 15), datetime.date(2021, 3, 19), 'AZ-Stopp', 'silver') + labeled_timeperiod(ax, datetime.date(2021, 3, 29), today, 'AZ-Stopp u. 60', 'lightgrey') def plot_vaccination_bar_graph_total_time(): - archive_plot_filename = '{}/vaccination_bar_graph_total_time'.format(archive_folder) - latest_plot_filename = '{}/vaccination_bar_graph_total_time'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_bar_graph_total_time' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -319,24 +352,18 @@ def plot_vaccination_bar_graph_total_time(): ax.set_xlabel('Datum') ax.set_ylabel('Tägliche Impfungen') + add_annotations(ax) - plt.savefig(archive_plot_filename + '.pdf') - plt.savefig(archive_plot_filename + '.png') - plt.savefig(latest_plot_filename + '.pdf') - plt.savefig(latest_plot_filename + '.png') + save_plot(plot_name) plt.close() - print('Created plot {} as pdf and png'.format(archive_plot_filename)) plot_vaccination_bar_graph_total_time() def plot_vaccination_bar_graph_total_time_by_week(): - archive_plot_filename = '{}/vaccination_bar_graph_total_time_by_week'.format(archive_folder) - latest_plot_filename = '{}/vaccination_bar_graph_total_time_by_week'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_bar_graph_total_time_by_week' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -359,6 +386,8 @@ def plot_vaccination_bar_graph_total_time_by_week(): bar1 = ax.bar(w, f, label='Wöchentliche Erstimpfungen', color='blue', width=6.8) bar2 = ax.bar(w, s, label='Wöchentliche Zweitimpfungen', color='lightblue', width=6.8, bottom=f) + i = 0 + for r1, r2 in zip(bar1, bar2): x = r1.get_x() + r1.get_width() / 2.0 @@ -375,6 +404,37 @@ def plot_vaccination_bar_graph_total_time_by_week(): plt.text(x, hg * 1000, f'{hg:5n} k'.replace('.', ' '), ha='center', va='bottom') + if i == 12: + # Woche der AstraZeneca-Aussetzung + plt.annotate('AZ-Stopp', (x, hg * 1000 + 50000), + xytext=(x, ax.get_ylim()[1]), + arrowprops={ + 'arrowstyle': '->' + }, + bbox={ + 'boxstyle': 'square', + 'fc': 'white', + 'ec': 'black' + }, + ha='center') + + if i == len(bar1) - 1: + plt.annotate('Diese Woche', (x, hg * 1000 + 50000), + xytext=(x, ax.get_ylim()[1]), + arrowprops={ + 'arrowstyle': '->', + 'relpos': (0, 0) + }, + bbox={ + 'boxstyle': 'square', + 'fc': 'white', + 'ec': 'black' + }, + ha='left') + + i = i + 1 + + ax.legend(loc='upper left') ax.get_xaxis().set_major_formatter(DateFormatter('%Y-w%W')) ax.get_xaxis().set_major_locator(WeekdayLocator(3, 2)) @@ -383,24 +443,15 @@ def plot_vaccination_bar_graph_total_time_by_week(): ax.set_xlabel('Datum') ax.set_ylabel('Wöchentliche Impfungen') - - plt.savefig(archive_plot_filename + '.pdf') - plt.savefig(archive_plot_filename + '.png') - plt.savefig(latest_plot_filename + '.pdf') - plt.savefig(latest_plot_filename + '.png') + save_plot(plot_name) plt.close() - print('Created plot {} as pdf and png'.format(archive_plot_filename)) - plot_vaccination_bar_graph_total_time_by_week() def plot_vaccination_bar_graph_total_time_two_bars(): - archive_plot_filename = '{}/vaccination_bar_graph_total_time_two_bars'.format(archive_folder) - latest_plot_filename = '{}/vaccination_bar_graph_total_time_two_bars'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_bar_graph_total_time_two_bars' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -429,24 +480,17 @@ def plot_vaccination_bar_graph_total_time_two_bars(): ax.set_xlabel('Datum') ax.set_ylabel('Tägliche Impfungen') + add_annotations(ax) - plt.savefig(archive_plot_filename + '.pdf') - plt.savefig(archive_plot_filename + '.png') - plt.savefig(latest_plot_filename + '.pdf') - plt.savefig(latest_plot_filename + '.png') + save_plot(plot_name) plt.close() - print('Created plot {} as pdf and png'.format(archive_plot_filename)) - plot_vaccination_bar_graph_total_time_two_bars() def plot_vaccination_bar_graph_compare_both_vaccinations(): - archive_plot_filename = '{}/vaccination_bar_graph_compare_both_vaccinations'.format(archive_folder) - latest_plot_filename = '{}/vaccination_bar_graph_compare_both_vaccinations'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_bar_graph_compare_both_vaccinations' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -476,23 +520,15 @@ def plot_vaccination_bar_graph_compare_both_vaccinations(): ax.set_xlabel('Datum') ax.set_ylabel('Tägliche Impfungen') - - plt.savefig(archive_plot_filename + '.pdf') - plt.savefig(archive_plot_filename + '.png') - plt.savefig(latest_plot_filename + '.pdf') - plt.savefig(latest_plot_filename + '.png') + save_plot(plot_name) plt.close() - print('Created plot {} as pdf and png'.format(archive_plot_filename)) - plot_vaccination_bar_graph_compare_both_vaccinations() def plot_cumulative_two_vaccinations(): - archive_plot_filename = '{}/cumulative_two_vaccinations'.format(archive_folder) - latest_plot_filename = '{}/cumulative_two_vaccinations'.format(site_folder) - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'cumulative_two_vaccinations' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -522,24 +558,18 @@ def plot_cumulative_two_vaccinations(): ax.set_xlabel('Datum') ax.set_ylabel('Kumulative Impfungen') + add_annotations(ax) - plt.savefig(archive_plot_filename + '.pdf') - plt.savefig(archive_plot_filename + '.png') - plt.savefig(latest_plot_filename + '.pdf') - plt.savefig(latest_plot_filename + '.png') + save_plot(plot_name) plt.close() - print('Created plot {} as pdf and png'.format(archive_plot_filename)) - plot_cumulative_two_vaccinations() def plot_cumulative_two_vaccinations_percentage(): - archive_plot_filename = '{}/cumulative_two_vaccinations_percentage'.format(archive_folder) - latest_plot_filename = '{}/cumulative_two_vaccinations_percentage'.format(site_folder) - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'cumulative_two_vaccinations_percentage' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -570,25 +600,19 @@ def plot_cumulative_two_vaccinations_percentage(): ax.set_xlabel('Datum') ax.set_ylabel('Kumulative Impfungen') + add_annotations(ax) - plt.savefig(archive_plot_filename + '.pdf') - plt.savefig(archive_plot_filename + '.png') - plt.savefig(latest_plot_filename + '.pdf') - plt.savefig(latest_plot_filename + '.png') + save_plot(plot_name) plt.close() - print('Created plot {} as pdf and png'.format(archive_plot_filename)) - plot_cumulative_two_vaccinations_percentage() def plot_people_between_first_and_second(): - archive_plot_filename = '{}/people_between_first_and_second'.format(archive_folder) - latest_plot_filename = '{}/people_between_first_and_second'.format(site_folder) - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'people_between_first_and_second' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -620,25 +644,18 @@ def plot_people_between_first_and_second(): ax.set_xlabel('Datum') ax.set_ylabel('Personen zwischen Erst- und Zweitimpfung') + add_annotations(ax) - plt.savefig(archive_plot_filename + '.pdf') - plt.savefig(archive_plot_filename + '.png') - plt.savefig(latest_plot_filename + '.pdf') - plt.savefig(latest_plot_filename + '.png') + save_plot(plot_name) plt.close() - print('Created plot {} as pdf and png'.format(archive_plot_filename)) - plot_people_between_first_and_second() def plot_vaccination_rate(): - archive_plot_filename = '{}/vaccination_rate'.format(archive_folder) - latest_plot_filename = '{}/vaccination_rate'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_rate' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -671,23 +688,17 @@ def plot_vaccination_rate(): ax.set_xlabel('Datum') ax.set_ylabel('Impfrate [Impfungen/Tag]') - plt.savefig(archive_plot_filename + '.pdf') - plt.savefig(archive_plot_filename + '.png') - plt.savefig(latest_plot_filename + '.pdf') - plt.savefig(latest_plot_filename + '.png') - plt.close() + add_annotations(ax) - print('Created plot {} as pdf and png'.format(archive_plot_filename)) + save_plot(plot_name) + plt.close() plot_vaccination_rate() def plot_vaccination_done_days(): - archive_plot_filename = '{}/vaccination_done_days'.format(archive_folder) - latest_plot_filename = '{}/vaccination_done_days'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_done_days' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -718,23 +729,17 @@ def plot_vaccination_done_days(): ax.set_xlabel('Datum') ax.set_ylabel('Tage, bis 70 % erreicht sind') - plt.savefig(archive_plot_filename + '.pdf') - plt.savefig(archive_plot_filename + '.png') - plt.savefig(latest_plot_filename + '.pdf') - plt.savefig(latest_plot_filename + '.png') - plt.close() + add_annotations(ax) - print('Created plot {} as pdf and png'.format(archive_plot_filename)) + save_plot(plot_name) + plt.close() plot_vaccination_done_days() def plot_vaccination_done_dates(): - archive_plot_filename = '{}/vaccination_done_dates'.format(archive_folder) - latest_plot_filename = '{}/vaccination_done_dates'.format(site_folder) - - if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: - print('Plot {} already exists'.format(archive_plot_filename)) + plot_name = 'vaccination_done_dates' + if not check_recreate_plot(plot_name): return fig, ax = plt.subplots(1) @@ -775,13 +780,10 @@ def plot_vaccination_done_dates(): ax.set_xlabel('Datum') ax.set_ylabel('Datum, an dem 70 % erreicht sind') - plt.savefig(archive_plot_filename + '.pdf') - plt.savefig(archive_plot_filename + '.png') - plt.savefig(latest_plot_filename + '.pdf') - plt.savefig(latest_plot_filename + '.png') - plt.close() + add_annotations(ax) - print('Created plot {} as pdf and png'.format(archive_plot_filename)) + save_plot(plot_name) + plt.close() plot_vaccination_done_dates() @@ -791,7 +793,7 @@ def render_dashboard(): stylesheet_filename = 'site/rki-dashboard.css' stylesheet_archive_filename = 'site/archive/{}/rki-dashboard.css'.format(filename_stand) - if os.path.isfile(dashboard_archive_filename) and not force_renew: + if os.path.isfile(dashboard_archive_filename) and not force_renew_dashboard: print('Dashboard {} already exists'.format(dashboard_archive_filename)) return