From 0bbd3a150ce5342ba4cf41aafdcaa044a8f2fa8e Mon Sep 17 00:00:00 2001 From: landrigun Date: Wed, 20 Apr 2022 13:28:28 +0000 Subject: [PATCH] adjust ticks date evenly for bar plots + delete 100% et 0% bar label (#2) --- drees.py | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/drees.py b/drees.py index 40a9ee9..bb185b2 100644 --- a/drees.py +++ b/drees.py @@ -506,35 +506,25 @@ def plot_bar_vaccine_status_distribution_by_age_field( """ np_vac_distri, np_unvac_distri = get_vaccine_status_distribution(np_data) # adjust the fig size to display correctly bars and labels - fig, ax = get_plot_fig(figsize=(22, 8)) + fig, ax = get_plot_fig(figsize=(22, 8), locator=md.WeekdayLocator()) - for idx_date in range(len(np_date)): + for idx_date, date in enumerate(np_date): vac_percent = np.round( np_vac_distri[idx_date, age_group.value, field.value] * 100, 2 ) unvac_percent = np.round( np_unvac_distri[idx_date, age_group.value, field.value] * 100, 2 ) - bar_vac = ax.bar(idx_date, vac_percent, color="b", label="Vaccinés") - ax.bar( - idx_date, unvac_percent, bottom=vac_percent, color="r", label="Non vaccinés" - ) - ax.bar_label( - bar_vac, label_type="edge", color="black", fontsize="7", fmt="%.0f" - ) - ax.set_ylim(top=105) # to display 100% label + bar_vac = ax.bar(date, vac_percent, color="b", label="Vaccinés") + ax.bar(date, unvac_percent, bottom=vac_percent, color="r", label="Non vaccinés") + if vac_percent not in (0, 100): + ax.bar_label( + bar_vac, label_type="edge", color="black", fontsize="6.5", fmt="%.0f" + ) ax.set_ylabel("%") ax.set_title(f"{age_group.label} - {field.label}") - # avoid displaying all dates - ax.set( - xticks=range(len(np_date)), - xticklabels=[ - d.strftime(DATE_FORMAT) if idx % 4 == 0 else "" - for idx, d in enumerate(np_date.astype(dt)) - ], - ) plt.legend(["Vaccinés", "Non vaccinés"], loc="upper right", frameon=True) save_and_close_fig(