Plot styling
MesPy builds its plots with Matplotlib. Choose a style for the overall look, then adjust individual elements when you need more control.
Matplotlib styles
Both histogram() and lin_fit() accept a style argument. A style sets the overall appearance: fonts, colors, grids, and line properties.
You can use:
Matplotlib styles such as
"ggplot"or"dark_background".MesPy’s
"mespy","report", or"column"styles, registered when you import MesPy.A path to a custom
.mplstylefile, supported by both functions.style=Noneto keep the currently active Matplotlib configuration.
Both functions currently default to style="mespy". A named style applies only during the call; it does not change the global configuration.
Run the examples in order. These small measurements describe position y at time x; sigma_y gives the position uncertainties.
from mespy import histogram, lin_fit
x = [0, 1, 2, 3, 4]
y = [0.6, 2.4, 4.7, 6.3, 8.8]
sigma_y = [0.3, 0.3, 0.3, 0.3, 0.3]
data = y
histogram(
data,
style="ggplot",
)
lin_fit(
x,
y,
sigma_y,
style="ggplot",
)
One style for the whole notebook
To give every plot the same look, including those you create directly with Matplotlib, activate a style once at the start of the notebook or script. After import mespy, the MesPy styles are available by name:
import matplotlib.pyplot as plt
plt.style.use("mespy")
The style stays active for the whole session. histogram() and lin_fit() still apply their own style argument during the call: with the default "mespy" the look does not change. If you activated another global style, for example plt.style.use("report"), pass style=None to keep it: MesPy still provides its own plot elements and labels.
Customizing a histogram
Use MesPy parameters
Set bins to choose the number of bars. Use xlabel, ylabel, and title for the text, and bar_color for the bars. Explicit values override the style for that call. show_mean and show_band control the mean line and the shaded band.
histogram(
data,
bins=4,
xlabel="Position [m]",
ylabel="Count",
title="Position measurements",
bar_color="teal",
show_band=False,
)
Continue editing with Matplotlib
histogram() returns a standard Matplotlib Figure and Axes. The figure is the whole image; the axes hold the plotted data, labels, and grid. Create the axes first and pass them with ax=, then edit, save, and show the figure: the notebook will display your changes too.
fig, ax = plt.subplots()
histogram(data, ax=ax)
ax.set_title("Position measurements")
ax.set_xlabel("Position [m]")
ax.set_ylabel("Count")
ax.grid(False)
fig.savefig("histogram.pdf", bbox_inches="tight")
plt.show()
MesPy creates the statistical visualization; Matplotlib lets you finish it. Save after editing to include your changes.
The optional save_path argument saves during the histogram() call. It does not create missing folders, and later edits do not update that saved file automatically.
Customizing a linear fit
lin_fit() accepts xlabel, ylabel, and title directly. It also has normalize_residuals to change how the lower panel displays residuals. Choose fonts, colors, grid settings, figure size, and line widths through style.
Use a custom style
For a reusable look, save this file as styles/lab.mplstyle in your project:
lab.mplstyle
font.size: 11
figure.figsize: 7, 5
axes.grid: True
grid.alpha: 0.25
lines.linewidth: 1.5
axes.prop_cycle: cycler('color', ['teal', 'darkorange'])
The first color is used for the data; the second is used for the fitted line and its band. Pass the file path to style:
result = lin_fit(
x,
y,
sigma_y,
xlabel="Time [s]",
ylabel="Position [m]",
title="Motion measurements",
style="styles/lab.mplstyle",
output="results/styled_fit",
)
output is a base path without an extension. MesPy creates the folder if needed and writes styled_fit.pdf and styled_fit.toml. The PDF is saved while the style is active, including any savefig.* settings in that style.
Choose your style and labels before calling lin_fit() to include them in the automatic export. Appearance options such as bar_color or title_fontsize belong to histogram(); lin_fit() does not accept them.
Edit the returned figure
lin_fit() returns a LinearFitResult, rather than a (fig, ax) pair. With the default plot=True, result.figure is a Matplotlib figure you can edit and save. With plot=False, it is None.
Its two axes are the fit panel followed by the residuals panel:
fig = result.figure
ax_fit, ax_residuals = fig.axes
ax_fit.set_title("Motion: final plot")
fig.savefig("results/styled_fit_edited.pdf", bbox_inches="tight")
This writes a separate PDF with your edits. It does not update the previously exported styled_fit.pdf or the TOML file. The fit results stay the same.