Back in Scrubs

Last week, I returned to my mandatory clinical internship at Karolinska University Hospital, after a wonderful seven month stint as stay-at-home dad. First stop - the emergency department.

I got curious and managed to find statistics on number of visits by diagnosis groups at the ED. I whipped up a plot and ran it through Claude Code for polish:

graphs/top_10_dx.png

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np
import textwrap

CUT_OFF = 10

df_raw = pd.read_csv("dx_huddinge_2024_eng.csv", delimiter=";")
df_raw = df_raw.sort_values("antal_besök", ascending=False).reset_index(drop=True)
df = df_raw[:CUT_OFF].copy()

total = df_raw["antal_besök"].sum()
cutoff_total = df["antal_besök"].sum()
cutoff_pct = cutoff_total / total * 100

# --- Color palette: sequential teal-to-sky gradient ---
colors = plt.cm.GnBu(np.linspace(0.85, 0.35, CUT_OFF))

fig, ax = plt.subplots(figsize=(13, 8))

bars = ax.bar(
    range(CUT_OFF),
    df["antal_besök"],
    width=0.6,
    color=colors,
    edgecolor="white",
    linewidth=0.8,
    zorder=3,
)

# Annotate with count + percentage
for i, bar in enumerate(bars):
    height = bar.get_height()
    pct = height / total * 100
    ax.text(
        bar.get_x() + bar.get_width() / 2,
        height + 60,
        f"{height:,}\n({pct:.1f}%)",
        ha="center", va="bottom",
        fontsize=8, color="#333333",
        linespacing=1.4,
    )

# Wrapped x-tick labels
wrapped_labels = [textwrap.fill(lbl, width=18) for lbl in df["diagnosgrupp"]]
ax.set_xticks(range(CUT_OFF))
ax.set_xticklabels(wrapped_labels, ha="right", rotation=35, fontsize=9)

# Grid and spines
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_alpha(0.3)
ax.yaxis.grid(True, alpha=0.35, linestyle="--", zorder=0)
ax.xaxis.grid(False)
ax.set_axisbelow(True)

ax.set_ylabel("Number of visits", fontsize=11)
ax.yaxis.set_major_formatter(mticker.FuncFormatter(lambda x, _: f"{int(x):,}"))
ax.set_ylim(0, df["antal_besök"].max() * 1.22)

# Title + subtitle with cumulative coverage
fig.suptitle(
    f"Top {CUT_OFF} Diagnosis Groups — Karolinska University Hospital ED 2024",
    fontsize=14, fontweight="bold", y=0.98,
)
ax.set_title(
    f"These {CUT_OFF} groups account for {cutoff_pct:.1f}% of all {total:,} visits",
    fontsize=10, color="#555555", pad=10,
)

plt.tight_layout()
plt.savefig("dx_huddinge_top10.png", dpi=150, bbox_inches="tight")
plt.show()