codex/past-exam-of-the-probability-statistics-and-options-i-course-of-the-university-of-oxford/2008-prufer-tree.py

past-exam-of-the-probability-statistics-and-options-i-course-of-the-university-of-oxford/2008-prufer-tree.py
"""Python 3.14; Matplotlib in ../pyproject.toml. Output in CWD."""
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
pos={2:(0,0),1:(-1,1),4:(-1,-1),5:(.1,1.25),6:(1,0),3:(2,0)}
edges=[(1,2),(3,6),(2,4),(2,5),(2,6)]
fig,ax=plt.subplots(figsize=(6,3.7),facecolor="white")
for a,b in edges:ax.plot([pos[a][0],pos[b][0]],[pos[a][1],pos[b][1]],color=".4",lw=2)
for n,(x,y) in pos.items():
 ax.scatter([x],[y],s=550,facecolor="white",edgecolor="C0",lw=2,zorder=3)
 ax.text(x,y,str(n),ha="center",va="center",zorder=4)
ax.set_title("Tree with Prüfer code (2, 6, 2, 2)")
ax.set_aspect("equal");ax.axis("off");ax.margins(.2)
fig.tight_layout();fig.savefig("2008-prufer-tree.png",dpi=150,facecolor="white")