Add option to not fix lims in plot

This commit is contained in:
Felix Blanke 2023-09-01 14:46:10 +02:00
parent dbf477ed17
commit ef8cab6330

21
wsgi.py
View File

@ -96,6 +96,7 @@ def plot(
total_targets: tuple[int, ...] = (1500, 2500, 3500),
alpha: float | None = None,
landesbez_str: str | None = None,
fix_lims: bool = True,
) -> str:
fig = plt.figure(dpi=300)
@ -125,7 +126,7 @@ def plot(
ls="--",
marker="o",
lw=1,
color="#e4004e" if bez is None else None,
color="#e4004e" if bez is None or not fix_lims else None,
markersize=4,
label=bez if bez is not None else "Bundesweit",
)
@ -152,14 +153,15 @@ def plot(
plt.title("Teilnahme an Digitaler Beschäftigtenbefragung")
plt.ylabel("# Teilnahmen")
max_val = df.sum(axis=1).max().item()
if fix_lims:
max_val = df.sum(axis=1).max().item()
nearest_target = np.array(total_targets, dtype=np.float32) - max_val
nearest_target[nearest_target <= 0] = np.inf
idx = np.argmin(nearest_target)
nearest_target = np.array(total_targets, dtype=np.float32) - max_val
nearest_target[nearest_target <= 0] = np.inf
idx = np.argmin(nearest_target)
ceil_val = max(max_val, total_targets[idx])
plt.ylim(0, ceil_val * 1.025)
ceil_val = max(max_val, total_targets[idx])
plt.ylim(0, ceil_val * 1.025)
plt.legend()
# use timezone offset to center tick labels
@ -184,8 +186,9 @@ def plot(
sec_ax.set_ylabel("# Teilnahmen [% Erfolg]")
sec_ax.yaxis.set_major_formatter(mtick.PercentFormatter())
for total_target in total_targets:
plt.axhline(y=total_target, color="#48a9be", linestyle="--")
if fix_lims:
for total_target in total_targets:
plt.axhline(y=total_target, color="#48a9be", linestyle="--")
plt.tight_layout()