# Histogramming: NumPy-like¶

pygram11.histogram(x, bins=10, range=None, weights=None, density=False, flow=False, omp='auto')[source]

Compute the histogram for the data x.

This function provides an API very simiar to numpy.histogram(). Keep in mind that the returns are different.

Parameters
• x (array_like) – Data to histogram.

• bins (int or sequence of scalars, optional) – If bins is an int, that many equal-width bins will be used to construct the histogram in the given range. If bins is a sequence, it must define a monotonically increasing array of bin edges. This allows for nonuniform bin widths.

• range ((float, float), optional) – The range over which the histogram is constructed. If a range is not provided then the default is (x.min(), x.max()). Values outside of the range are ignored. If bins is a sequence, this options is ignored.

• weights (array_like, optional) – An array of weights associated to each element of x. Each value of x will contribute its associated weight to the bin count. If argument is two-dimensional then the function will return histogram data for all weight variations. The first dimension of the shape of the weights (if multiple variations) must be the shape of x.

• density (bool) – normalize histogram bins as value of PDF such that the integral over the range is 1.

• flow (bool) – if True the under and overflow bin contents are added to the first and last bins, respectively

• omp (bool or str) – if True, use OpenMP if available; if “auto” (and OpenMP is available), enables OpenMP if len(x) > 10^4 for fixed width and > 10^3 for variaxsble width bins.

Returns

Examples

A histogram of x with 20 bins between 0 and 100.

>>> h, __ = fix1d(x, bins=20, range=(0, 100))


The same data, now histogrammed weighted & accelerated with OpenMP.

>>> h, err = histogram(x, bins=20, range=(0, 100), omp=True)


Multiple histograms of x with 50 bins between 0 and 100; using 20 different weight variations:

>>> x = np.random.randn(10000)
>>> twenty_weights = np.random.rand(x.shape, 20)
>>> h, err = histogram(x, bins=50, range=(-3, 3), weights=twenty_weights, omp=True)


h and err are now shape (50, 20). Each column represents the histogram of the data with the respective weight.

A histogram of x where the edges are defined by the list [1, 5, 10, 12]:

>>> h, __ = var1d(x, bins=[1, 5, 10, 12])


The same data, now weighted and accelerated with OpenMP:

>>> h, err = histogram(x, bin=[1, 5, 10, 12], weights=w, omp=True)


Multiple histograms of x (with 9 variable width bins); using 20 different weight variations (also shifting under and overflow contents):

>>> x = np.random.randn(10000)
>>> bins = [-2, -1.5, -1.2, -1, -0.8, 0, 1.2, 1.5, 3.0]
>>> twenty_weights = np.random.rand(x.shape, 20)
>>> h, err = histogram(x, bins=bins, weights=twenty_weights, flow=True, omp=True)


h and err are now shape (9, 20). Each column represents the histogram of the data with the respective weight.

pygram11.histogram2d(x, y, bins=10, range=None, weights=None, omp=False)[source]

Compute the two-dimensional histogram for the data (x, y).

This function provides an API very simiar to numpy.histogram2d(). Keep in mind that the returns are different.

Parameters
• x (array_like) – Array representing the x coordinate of the data to histogram.

• y (array_like) – Array representing the y coordinate of the data to histogram.

• bins (int or array_like or [int, int] or [array, array], optional) –

The bin specification:
• If int, the number of bins for the two dimensions (nx = ny = bins).

• If array_like, the bin edges for the two dimensions (x_edges = y_edges = bins).

• If [int, int], the number of bins in each dimension (nx, ny = bins).

• If [array_like, array_like], the bin edges in each dimension (x_edges, y_edges = bins).

• range (array_like, shape(2,2), optional) – The edges of this histogram along each dimension. If bins is not integral, then this parameter is ignored. If None, the default is [[x.min(), x.max()], [y.min(), y.max()]].

• weights (array_like) – An array of weights associated to each element $$(x_i, y_i)$$ pair. Each pair of the the data will contribute its associated weight to the bin count.

• omp (bool) – Use OpenMP if available

Returns