In this guide Load your data

Examples / mespy.histogram · mespy.lin_fit

Basic workflow

Load a CSV with pandas, then create a histogram with mespy.histogram and a linear fit with mespy.lin_fit. Find the PDF plots and TOML fit results in your chosen output folder.

Load your data

Save measurements.csv in your project’s data/ folder. It has three columns: x, y, and sigma_y, the uncertainty on each y value.

x,y,sigma_y
1.0,2.85,0.25
2.0,4.90,0.25
3.0,6.88,0.25
4.0,9.18,0.25
5.0,11.08,0.25
6.0,13.42,0.25
7.0,15.35,0.25
8.0,17.63,0.25
9.0,19.55,0.25
10.0,21.92,0.25

Run the code from your project folder. pandas reads the CSV into a DataFrame named df:

import pandas as pd

from mespy import histogram, lin_fit

df = pd.read_csv("data/measurements.csv")

Access any column with df["column_name"]. For example, df["y"] contains the measured values.

Create a histogram

histogram saves a PDF at save_path. The results/ folder must already exist: histogram does not create missing folders, so create it before saving.

from pathlib import Path

Path("results").mkdir(exist_ok=True)

histogram(
    df["y"],
    save_path="results/histogram.pdf",
)

Perform a linear fit

Use the same DataFrame. Pass sigma_y to include the measurement uncertainties in the fit.

lin_fit(
    df["x"],
    df["y"],
    sigma_y=df["sigma_y"],
    output="results/linear_fit",
)

output sets the destination and base filename, without an extension. This call creates:

  • linear_fit.pdf — the fit plot.

  • linear_fit.toml — the numerical fit results.

Saved files

Your project now contains:

project/
├── data/
│   └── measurements.csv
└── results/
    ├── histogram.pdf
    ├── linear_fit.pdf
    └── linear_fit.toml

Both functions save PDF plots. lin_fit also saves numerical results in TOML, a text file you can open in any text editor.