histogram2d#

named_arrays.histogram2d(x, y, bins, axis=None, min=None, max=None, density=False, weights=None)#

A thin wrapper around numpy.histogram2d() which adds an axis argument.

Parameters:
  • x (AbstractScalarArray) – An array containing the x coordinates of the points to be sampled.

  • y (AbstractScalarArray) – An array containing the y coordinates of the points to be sampled.

  • bins (dict[str, int] | AbstractCartesian2dVectorArray) –

    The bin specification of the histogram:
    • If bins is a dictionary, the keys are interpreted as the axis names and the values are the number of bins along each axis. This dictionary must have exactly two keys.

    • If bins is a 2D Cartesian vector, each component of the vector represents the bin edges in each dimension.

  • axis (None | str | Sequence[str]) – The logical axes along which to histogram the data points. If None (the default), the histogram will be computed along all the axes of x and y.

  • min (None | AbstractScalarArray | AbstractCartesian2dVectorArray) – The lower boundary of the histogram along each dimension. If None (the default), the minimum of x and y is used.

  • max (None | AbstractScalarArray | AbstractCartesian2dVectorArray) – The upper boundary of the histogram along each dimension. If None (the default), the maximum of x and y is used.

  • density (bool) – If False (the default), returns the number of samples in each bin. If True, returns the probability density in each bin.

  • weights (None | AbstractScalarArray) – An optional array weighting each sample.

Return type:

FunctionArray[Cartesian2dVectorArray, ScalarArray]

Examples

Construct a 2D histogram with constant bin width.

import numpy as np
import matplotlib.pyplot as plt
import named_arrays as na

# Define the bin edges
bins = dict(x=6, y=5)

# Define random points to collect into a histogram
x = na.random.normal(0, 2, shape_random=dict(h=101))
y = na.random.normal(0, 3, shape_random=dict(h=101))

# Compute the 2D histogram
hist = na.histogram2d(x, y, bins=bins)

# Plot the resulting histogram
fig, ax = plt.subplots()
na.plt.pcolormesh(C=hist);
../_images/named_arrays.histogram2d_0_0.png

Construct a 2D histogram with variable bin width.

import matplotlib.pyplot as plt
import named_arrays as na

# Define the bin edges
bins = na.Cartesian2dVectorArray(
    x=np.square(na.linspace(0, 2, axis="x", num=6)),
    y=np.square(na.linspace(0, 2, axis="y", num=5)),
)

# Define random points to collect into a histogram
x = na.random.normal(0, 2, shape_random=dict(h=101))
y = na.random.normal(0, 3, shape_random=dict(h=101))

# Compute the 2D histogram
hist = na.histogram2d(x, y, bins=bins)

# Plot the resulting histogram
fig, ax = plt.subplots()
na.plt.pcolormesh(C=hist);
../_images/named_arrays.histogram2d_1_0.png