bhylu.com

Advertisements

SciPy Plotting Results

Create a labelled figure from the same checked data and interpolator.

How it works

A plot can reveal mismatched axes, odd interpolation or an unexpected domain, but it does not replace numeric checks. This local-only lesson uses Matplotlib Agg to save an original static figure without a desktop window. The output path is refused if it already exists, so rerunning in a folder with a personal file cannot silently replace it.

Run the code

This example runs locally: it needs Matplotlib 3.11.2 and saves an original labelled PNG in your learning folder.

The reviewed local environment is Python 3.12.14, NumPy 2.5.4 and SciPy 1.18.0. The browser uses Pyodide 0.27.7, NumPy 2.0.2 and SciPy 1.14.1. Use a separate virtual environment for local work, then run python plotting-results.py. Apache, MySQL and PHP are not required. Environment setup.

Download the complete source

Example

plotting-results.py

import numpy as np
import scipy

from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from scipy.interpolate import PchipInterpolator

x = np.array([0., 1., 2., 3.])
y = np.array([1., 3., 5., 7.])
curve = PchipInterpolator(x, y, extrapolate=False)
query = np.linspace(0., 3., 121)
np.testing.assert_allclose(curve(query), 1. + 2. * query, atol=1e-12, rtol=0)
target = Path("calibration.png")
if target.exists():
    raise FileExistsError("Refusing existing calibration.png")
figure, axis = plt.subplots(figsize=(8, 4.5))
axis.scatter(x, y, label="Synthetic readings", color="#0078d4")
axis.plot(query, curve(query), label="PCHIP", color="#0f3464")
axis.set(xlabel="Input setting (unitless)", ylabel="Synthetic response (unitless)", title="Observatory sensor-bench calibration")
axis.legend()
figure.tight_layout()
figure.savefig(target, dpi=120)
plt.close(figure)
print("Saved calibration.png (960 x 540)")

Expected result

Saved calibration.png (960 x 540)

The known affine relationship checks every plotted query. The chart identifies its synthetic data, axis meanings and interpolator. Keep the reviewed source with the figure so someone can reproduce it, and do not crop away labels when sharing it. Matplotlib is an additional local dependency rather than a promised feature of the text-only browser runner.

Original synthetic observatory calibration samples and a checked PCHIP line, with labelled unitless input and response axes.
Generated from this lesson source; synthetic teaching data.

For precise contracts and version differences use the official SciPy documentation. Synthetic fixtures do not establish real instrument accuracy or population conclusions.