diff --git a/dashboard_template.xhtml b/dashboard_template.xhtml index 5256a41..35a6eb7 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 3c5cf69..d47c463 100644 --- a/plot.py +++ b/plot.py @@ -37,8 +37,7 @@ print_today = today.isoformat() filename_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S") -force_renew_plots = True -force_renew_dashboard = True +force_renew = 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) @@ -286,47 +285,15 @@ 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(): - plot_name = 'vaccination_bar_graph_total_time' - if not check_recreate_plot(plot_name): + 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)) return fig, ax = plt.subplots(1) @@ -352,18 +319,24 @@ def plot_vaccination_bar_graph_total_time(): ax.set_xlabel('Datum') ax.set_ylabel('Tägliche Impfungen') - add_annotations(ax) - save_plot(plot_name) + 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() + 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(): - plot_name = 'vaccination_bar_graph_total_time_by_week' - if not check_recreate_plot(plot_name): + 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)) return fig, ax = plt.subplots(1) @@ -386,8 +359,6 @@ 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 @@ -404,37 +375,6 @@ 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)) @@ -443,15 +383,24 @@ def plot_vaccination_bar_graph_total_time_by_week(): ax.set_xlabel('Datum') ax.set_ylabel('Wöchentliche Impfungen') - save_plot(plot_name) + + 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() + 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(): - plot_name = 'vaccination_bar_graph_total_time_two_bars' - if not check_recreate_plot(plot_name): + 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)) return fig, ax = plt.subplots(1) @@ -480,17 +429,24 @@ def plot_vaccination_bar_graph_total_time_two_bars(): ax.set_xlabel('Datum') ax.set_ylabel('Tägliche Impfungen') - add_annotations(ax) - save_plot(plot_name) + 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() + 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(): - plot_name = 'vaccination_bar_graph_compare_both_vaccinations' - if not check_recreate_plot(plot_name): + 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)) return fig, ax = plt.subplots(1) @@ -520,15 +476,23 @@ def plot_vaccination_bar_graph_compare_both_vaccinations(): ax.set_xlabel('Datum') ax.set_ylabel('Tägliche Impfungen') - save_plot(plot_name) + + 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() + 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) - plot_name = 'cumulative_two_vaccinations' - if not check_recreate_plot(plot_name): + if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: + print('Plot {} already exists'.format(archive_plot_filename)) return fig, ax = plt.subplots(1) @@ -558,18 +522,24 @@ def plot_cumulative_two_vaccinations(): ax.set_xlabel('Datum') ax.set_ylabel('Kumulative Impfungen') - add_annotations(ax) - save_plot(plot_name) + 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() + 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) - plot_name = 'cumulative_two_vaccinations_percentage' - if not check_recreate_plot(plot_name): + if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: + print('Plot {} already exists'.format(archive_plot_filename)) return fig, ax = plt.subplots(1) @@ -600,19 +570,25 @@ def plot_cumulative_two_vaccinations_percentage(): ax.set_xlabel('Datum') ax.set_ylabel('Kumulative Impfungen') - add_annotations(ax) - save_plot(plot_name) + 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() + 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) - plot_name = 'people_between_first_and_second' - if not check_recreate_plot(plot_name): + if os.path.isfile(archive_plot_filename + '.pdf') and not force_renew: + print('Plot {} already exists'.format(archive_plot_filename)) return fig, ax = plt.subplots(1) @@ -644,18 +620,25 @@ def plot_people_between_first_and_second(): ax.set_xlabel('Datum') ax.set_ylabel('Personen zwischen Erst- und Zweitimpfung') - add_annotations(ax) - save_plot(plot_name) + 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() + print('Created plot {} as pdf and png'.format(archive_plot_filename)) + plot_people_between_first_and_second() def plot_vaccination_rate(): - plot_name = 'vaccination_rate' - if not check_recreate_plot(plot_name): + 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)) return fig, ax = plt.subplots(1) @@ -688,17 +671,23 @@ def plot_vaccination_rate(): ax.set_xlabel('Datum') ax.set_ylabel('Impfrate [Impfungen/Tag]') - add_annotations(ax) - - save_plot(plot_name) + 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() + print('Created plot {} as pdf and png'.format(archive_plot_filename)) + plot_vaccination_rate() def plot_vaccination_done_days(): - plot_name = 'vaccination_done_days' - if not check_recreate_plot(plot_name): + 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)) return fig, ax = plt.subplots(1) @@ -729,17 +718,23 @@ def plot_vaccination_done_days(): ax.set_xlabel('Datum') ax.set_ylabel('Tage, bis 70 % erreicht sind') - add_annotations(ax) - - save_plot(plot_name) + 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() + print('Created plot {} as pdf and png'.format(archive_plot_filename)) + plot_vaccination_done_days() def plot_vaccination_done_dates(): - plot_name = 'vaccination_done_dates' - if not check_recreate_plot(plot_name): + 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)) return fig, ax = plt.subplots(1) @@ -780,11 +775,14 @@ def plot_vaccination_done_dates(): ax.set_xlabel('Datum') ax.set_ylabel('Datum, an dem 70 % erreicht sind') - add_annotations(ax) - - save_plot(plot_name) + 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() + print('Created plot {} as pdf and png'.format(archive_plot_filename)) + plot_vaccination_done_dates() def render_dashboard(): @@ -793,7 +791,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_dashboard: + if os.path.isfile(dashboard_archive_filename) and not force_renew: print('Dashboard {} already exists'.format(dashboard_archive_filename)) return