from pathlib import Path import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt output = Path("headless-plot.png") fig, ax = plt.subplots(layout="constrained") ax.plot([1, 2, 3, 4], [2, 4, 3, 5], marker="o") ax.set_title("Headless Matplotlib") ax.set_xlabel("Run") ax.set_ylabel("Value") fig.savefig(output, dpi=150) plt.close(fig) print(f"backend: {matplotlib.get_backend()}") print(f"saved: {output.name}") print(f"bytes: {output.stat().st_size}")