Overview
Purpose
Draw a histogram of an experimental sample, with options to highlight the mean, the +- 1 sigma band, the legend, the grid, and figure saving.
Parameters
x: data to histogram.ddof: parameter passed tostandard_deviationfor the band.style:Noneuses the currentrcParams,"mespy"loads the package style, and any other string is passed to Matplotlib as a style name.bins: number of bins, automatic algorithm, or array of edges.bin_width: fixed bin width. It cannot be used together withbinsother than"auto".hist_range:(xmin, xmax)pair used as the histogram range.label,xlabel,ylabel,title: text labels.show_bin_ticks,tick_rotation, anddecimals: control x-axis formatting.decimalsmust be an integer between 0 and 20.show_mean,show_band,show_legend,show_grid: enable or disable plot elements.xlim,ylim: explicit axis limits.ax: existing matplotlib axis to reuse.figsize,dpi, andsave_path: parameters for creating and saving the figure.figsizeanddpiare passed to figure creation only when explicitly provided.title_fontsize,title_pad,legend_fontsize, andlegend_loc: targeted overrides for the title and legend. If left asNone, the function uses the active style.bar_colorandedgecolor: targeted overrides for bar colours. If left asNone, the function uses the values supplied by the active style orrcParams.mean_color,band_color,hist_alpha,band_alpha,grid_alpha, andmean_symbol: control the mean line, the+- 1 sigmaband, transparency, and the legend symbol.grid_alpha=Noneleaves the grid to the active style.
Returns
A (fig, ax) tuple with the figure and axis on which the histogram was drawn.
Errors and exceptions
ValueErrorifxis empty, not one-dimensional, or contains non-finite values.ValueErrorifxlim,ylim, orhist_rangeare not valid pairs of finite values.ValueErrorifhist_rangedoes not satisfyxmin < xmax.ValueErrorifbin_width <= 0.ValueErrorifbin_widthandbinsare used together incompatibly.ValueErrorifdecimalsis not a valid integer between 0 and 20.ValueErroriffigsizeis not a positive pair when the figure is created internally.
Example
import numpy as np
from mespy import histogram
x = np.random.normal(loc=0.0, scale=1.0, size=100)
fig, ax = histogram(
x,
bins=10,
style="mespy",
xlabel="x",
title="Distribuzione delle misure",
show_band=True,
)
Notes
The style is applied through
_style_context, the same helper also used bylin_fit.If
axisNone, the function creates a new figure withplt.subplots.If you pass
ax, the returned figure isax.get_figure().Validation of limits, figure size, and
decimalsgoes through_validate_axis_limits,_validate_figsize, and_validate_decimals.save_pathalways saves withbbox_inches="tight";dpiis added to saving only when it is explicitly provided.