# Position against grade, and the grade price of one consecutive position.
fig, (axA, axB) = plt.subplots(1, 2, figsize=(8.6, 3.6))
# Panel A: the staircase. Steep where schools are scarce, flat where they crowd.
axA.plot(grades, ranks, ".", color=BLUE, ms=2.5, alpha=0.6)
axA.invert_yaxis()
axA.set_xlim(55, 172)
axA.set_xlabel("average grade (0-200)")
axA.set_ylabel("position in the ranking")
# The one position that costs almost five grades.
g_top = grade(1) - grade(2)
axA.plot([grade(2), grade(1)], [1, 1], color=ORANGE, lw=2.0)
axA.annotate(f"one position apart: {g_top:.2f} grades",
xy=(grade(2) + 0.5, 1), xytext=(133, 62), fontsize=7.5,
color=ORANGE, ha="left", va="center",
arrowprops=dict(arrowstyle="-", lw=0.7, color=ORANGE))
arrows = dict(arrowstyle="-", lw=0.7, color=MUTED)
labels = [("N. S. do Ros\u00e1rio (n=277)", 105, 26, grade(1) - 1.5, 1),
("Novo da Maia (n=134)", 100, 46, grade(2) - 1.5, 2),
("D. Diogo de Sousa (n=334)", 93, 80, grade(3) - 1.5, 3),
("Efanor (n=162)", 88, 120, grade(4) - 1.5, 4)]
for text, tx, ty, px, py in labels:
axA.annotate(text, (px, py), xytext=(tx, ty), fontsize=7, color=INK2,
ha="right", va="center", arrowprops=arrows)
# The ten gaps that cost a fraction of a grade altogether.
spans = grades[: -10] - grades[10:]
j = int(np.argmin(spans))
lo_rank = int(ranks[j])
axA.plot([grades[j], grades[j]], [lo_rank, lo_rank + 10], color=ORANGE, lw=2.0)
axA.annotate(f"ten positions apart: {spans[j]:.2f} grades", xy=(grades[j] + 0.6, lo_rank + 5),
xytext=(115, 330), fontsize=7.5,
color=ORANGE, ha="left", va="center",
arrowprops=dict(arrowstyle="-", lw=0.7, color=ORANGE))
# Panel B: what one position costs across the whole table (log scale).
gaps = -np.diff(grades)
axB.bar(ranks[:-1], gaps, width=1.0, color=BLUE, alpha=0.75)
axB.bar(1, gaps[0], width=1.0, color=ORANGE, zorder=5)
axB.set_yscale("log")
axB.set_ylim(5e-3, 20)
axB.set_xlabel("position in the ranking")
axB.set_ylabel("grades between one position and the next")
axB.annotate("4.84 points", xy=(1.8, gaps[0]),
xytext=(35, 8), fontsize=7.5, color=ORANGE, ha="left", va="center",
arrowprops=dict(arrowstyle="-", lw=0.7, color=ORANGE))
k = int(np.argmax(gaps))
axB.annotate("6.23 points", xy=(ranks[k], gaps[k]),
xytext=(300, 9), fontsize=7.5, color=ORANGE, ha="left", va="center",
arrowprops=dict(arrowstyle="-", lw=0.7, color=ORANGE))
fig.tight_layout()