Step 2 — plot

Reads the settings + the simulation output and saves a scatter plot of the darts (inside vs outside the circle) into this job’s outdir. Uses display() so the figure renders when papermill executes the notebook headlessly.

NOTE that the cell below has a tag: parameters.

settings_path = "settings.json"
import json
from pathlib import Path
import matplotlib
import matplotlib.pyplot as plt
import nb2slurm

if nb2slurm.on_hpc():
    display("on HPC")
    matplotlib.use("Agg")  # headless backend: no display needed on HPC

settings = nb2slurm.Settings.load(settings_path)
outdir = Path(settings["outdir"])
result = json.loads((outdir / "result.json").read_text())
points = json.loads((outdir / "points.json").read_text())
fig, ax = plt.subplots(figsize=(5, 5))
xs_in = [x for x, y, i in points if i]
ys_in = [y for x, y, i in points if i]
xs_out = [x for x, y, i in points if not i]
ys_out = [y for x, y, i in points if not i]
ax.scatter(xs_in, ys_in, s=2, color="tab:blue", label="inside")
ax.scatter(xs_out, ys_out, s=2, color="tab:red", label="outside")
ax.set_aspect("equal")
ax.set_title(
    f"pi ~= {result['pi']:.4f}  (n={result['n_samples']}, seed={result['seed']})"
)
ax.legend(loc="upper right")

fig_path = outdir / "scatter.png"
fig.savefig(fig_path, dpi=100, bbox_inches="tight")
print(f"saved {fig_path}")
display(fig)