adjust ticks date evenly for bar plots + delete 100% et 0% bar label (#2)

This commit is contained in:
landrigun 2022-04-20 13:28:28 +00:00
parent 7169a6f673
commit 0bbd3a150c

View File

@ -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"
)
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="7", fmt="%.0f"
bar_vac, label_type="edge", color="black", fontsize="6.5", fmt="%.0f"
)
ax.set_ylim(top=105) # to display 100% label
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(