hist.basehist module#

class hist.basehist.BaseHist(arg: dict[str, Any], /, *, data: NDArray[Any] | None = ..., metadata: Any = ..., label: str | None = ..., name: str | None = ...)#
class hist.basehist.BaseHist(arg: Self | bh.Histogram[S], /, *, data: NDArray[Any] | None = ..., metadata: Any = ..., label: str | None = ..., name: str | None = ...)
class hist.basehist.BaseHist(*axes: AxisProtocol | tuple[int, float, float] | Storage | str, storage: S = ..., metadata: Any = ..., data: NDArray[Any] | None = ..., label: str | None = ..., name: str | None = ...)

Bases: Histogram[S], Generic[S]

property T: Self#
density() → NDArray[Any]#

Density NumPy array.

expand_cats(*, name: Callable[..., str] | None = None) → dict[str, Self]#

Expand all categorical axes into a dict of histograms.

One histogram is produced for every combination of categories. The name callable receives the category values, one per categorical axis, and returns the key; the default joins them with _. Keys must be unique across all combinations, including empty groups. A duplicate key raises ValueError; pass name to choose unique keys.

fill(*args: ArrayLike, weight: ArrayLike | None = None, sample: ArrayLike | None = None, threads: int | None = None, **kwargs: ArrayLike) → Self#

Insert data into the histogram using names and indices, return a Hist object.

fill_flattened(*args: Any, weight: Any | None = None, sample: Any | None = None, threads: int | None = None, **kwargs: Any) → Self#
classmethod from_columns(data: Mapping[str, ArrayLike], axes: Sequence[str | AxisProtocol], *, weight: str | None = None, storage: hist.storage.Storage | None = None) → Self#
integrate(name: int | str, i_or_list: list[str | int], j: InnerIndexing | None = None) → Self#
integrate(name: int | str, i_or_list: InnerIndexing | None = None, j: InnerIndexing | None = None) → IntHists | int
integrate(name: int | str, i_or_list: InnerIndexing | None = None, j: InnerIndexing | None = None) → FloatHists | float
integrate(name: int | str, i_or_list: InnerIndexing | None = None, j: InnerIndexing | None = None) → ListHists | list[float]
integrate(name: int | str, i_or_list: InnerIndexing | None = None, j: InnerIndexing | None = None) → WeightHists | WeightedSum
integrate(name: int | str, i_or_list: InnerIndexing | None = None, j: InnerIndexing | None = None) → MeanHists | Mean
integrate(name: int | str, i_or_list: InnerIndexing | None = None, j: InnerIndexing | None = None) → WeightedMeanHists | WeightedMean
integrate(name: int | str, i_or_list: InnerIndexing | None = None, j: InnerIndexing | None = None) → Self | int | float | list[float] | bh.accumulators.Accumulator
plot(*args: Any, overlay: str | None = None, **kwargs: Any) → Hist1DArtists | Hist2DArtists#

Plot the histogram with mplhep, picking a 1D or 2D plot automatically.

  • 1D histograms, and 2D histograms with one continuous and one categorical axis (or with overlay given), are drawn with plot1d(), which wraps mplhep.histplot. For 2D histograms, one line is drawn per bin of the overlay axis.

  • Other 2D histograms are drawn with plot2d(), which wraps mplhep.hist2dplot.

  • Histograms with more than two dimensions must be projected first.

Parameters:
  • *args (Any) – Forwarded to plot1d() or plot2d(); both only take keyword arguments, so pass options by name.

  • overlay (str, optional) – Name of the axis to overlay for a 2D histogram. Giving it forces a 1D overlay plot. If None, a 2D histogram with one continuous and one categorical axis overlays the categorical axis.

  • **kwargs (Any) –

    Forwarded to plot1d() (ax, legend, then mplhep.histplot) or plot2d() (ax, then mplhep.hist2dplot). Any remaining keywords go to matplotlib, and ls/*_ls are accepted as aliases for linestyle/*_linestyle. Commonly used mplhep.histplot options include:

    • histtype: "step" (default), "fill", "errorbar", or another type supported by the installed mplhep.

    • yerr: True for Poisson/sqrt(N) errors, or an array.

    • w2method: "poisson", "sqrt", or a callable.

    • stack: stack the overlaid histograms.

    • density/binwnorm: normalize to unit area/to bin width.

    • flow: how to show under/overflow bins, e.g. "hint", "show", "sum" or "none".

    • label: legend label; for an overlay, a single string or one label per category (defaults to the category names).

    • sort: order overlaid histograms by "label" or "yield".

    Commonly used mplhep.hist2dplot options include cbar, cmin/cmax, labels (True prints bin values) and flow. See the mplhep documentation for the full list.

Returns:

The matplotlib artists returned by mplhep (a list of Hist1DArtists for 1D plots).

Return type:

Hist1DArtists or Hist2DArtists

Raises:

NotImplementedError – If the histogram has more than two dimensions.

plot1d(*, ax: matplotlib.axes.Axes | None = None, overlay: str | int | None = None, legend: bool = True, **kwargs: Any) → Hist1DArtists#

Plot1d method for BaseHist object.

Parameters:
  • ax (matplotlib.axes.Axes, optional) – Axes to plot on. If None, uses current axes or creates new ones.

  • overlay (str or int, optional) – Name or index of the axis to overlay. If None, automatically selects the first discrete axis for multi-dimensional histograms.

  • legend (bool, default True) – Whether to automatically add a legend when plotting stacked categories. The legend title is set from the axis label if available.

  • **kwargs (Any) – Additional keyword arguments passed to mplhep.histplot (for example histtype, yerr, stack, density, flow or label; see plot()), and from there to matplotlib.

Returns:

The matplotlib artists created by the plot.

Return type:

Hist1DArtists

plot2d(*, ax: matplotlib.axes.Axes | None = None, **kwargs: Any) → Hist2DArtists#

Plot2d method for BaseHist object.

Parameters:
  • ax (matplotlib.axes.Axes, optional) – Axes to plot on. If None, uses current axes or creates new ones.

  • **kwargs (Any) – Additional keyword arguments passed to mplhep.hist2dplot, such as cbar, cmin/cmax, labels or flow, and from there to matplotlib’s pcolormesh.

Returns:

The matplotlib artists created by the plot.

Return type:

Hist2DArtists

plot2d_full(*, ax_dict: dict[str, matplotlib.axes.Axes] | None = None, **kwargs: Any) → tuple[Hist2DArtists, Hist1DArtists, Hist1DArtists]#

Plot2d_full method for BaseHist object.

Pass a dict of axes to ax_dict, otherwise, the current figure will be used.

plot_pie(*, ax: matplotlib.axes.Axes | None = None, **kwargs: Any) → Any#
plot_pull(func: Callable[[np.typing.NDArray[Any]], np.typing.NDArray[Any]] | str, *, ax_dict: dict[str, matplotlib.axes.Axes] | None = None, **kwargs: Any) → tuple[FitResultArtists, RatiolikeArtists]#

Plot a 1D histogram with a fitted model and a pull subplot.

Return a tuple of artists following a structure of (main_ax_artists, subplot_ax_artists)

Parameters:
  • func (collections.abc.Callable or str) – A model to fit to this histogram. The strings "gauss", "gaus", and "normal" select a Gaussian model with initial parameter estimates derived from the histogram. Other strings are Python expressions in x, such as "a * x + b", where a and b are fit parameters. NumPy is available as np. Only use trusted expressions, since they are evaluated as Python code.

  • ax_dict (dict[str, matplotlib.axes.Axes]) – Axes for the histogram and pull subplot, keyed by "main_ax" and "pull_ax", respectively. Both keys are required when passing a nonempty dictionary. If omitted or empty, both axes are created in the current figure.

plot_ratio(other: hist.BaseHist[Any] | Callable[[np.typing.NDArray[Any]], np.typing.NDArray[Any]] | str, *, ax_dict: dict[str, matplotlib.axes.Axes] | None = None, **kwargs: Any) → tuple[MainAxisArtists, RatiolikeArtists]#

Plot a 1D histogram and its ratio to another histogram or a fitted model.

Return a tuple of artists following a structure of (main_ax_artists, subplot_ax_artists)

Parameters:
  • other (hist.BaseHist, collections.abc.Callable, or str) – A histogram to compare with, or a model to fit to this histogram. The strings "gauss", "gaus", and "normal" select a Gaussian model with initial parameter estimates derived from the histogram. Other strings are Python expressions in x, such as "a * x + b", where a and b are fit parameters. NumPy is available as np. Only use trusted expressions, since they are evaluated as Python code.

  • ax_dict (dict[str, matplotlib.axes.Axes]) – Axes for the histogram and ratio subplot, keyed by "main_ax" and "ratio_ax", respectively. Both keys are required when passing a nonempty dictionary. If omitted or empty, both axes are created in the current figure.

profile(axis: int | str) → Self#

Returns a profile (Mean/WeightedMean) histogram from a normal histogram with N-1 axes. The axis given is profiled over and removed from the final histogram.

project(*args: int | str, flow: bool = True) → Self#

Projection of axis idx.

show(**kwargs: Any) → Any#

Pretty print histograms to the console.

sort(axis: int | str, key: Callable[[int], SupportsLessThan] | Callable[[str], SupportsLessThan] | None = None, reverse: bool = False) → Self#

Sort a categorical axis.

stack(axis: int | str) → Stack[S]#

Returns a stack from a normal histogram axes.

class hist.basehist.SupportsLessThan(*args, **kwargs)#

Bases: Protocol

hist.basehist.process_mistaken_quick_construct(axes: Sequence[AxisProtocol | tuple[int, float, float] | ConstructProxy[Any]]) → Generator[AxisProtocol | tuple[int, float, float], None, None]#