mindspore.numpy.histogram2d(x, y, bins=10, range=None, weights=None, density=False)[source]

Computes the multidimensional histogram of some data.


Deprecated numpy argument normed is not supported.

  • x (Union[list, tuple, Tensor]) – An array with shape (N,) containing the x coordinates of the points to be histogrammed.

  • y (Union[list, tuple, Tensor]) – An array with shape (N,) containing the y coordinates of the points to be histogrammed.

  • bins (Union[int, tuple, list], 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, array], the bin edges in each dimension (x_edges, y_edges = bins).

    A combination [int, array] or [array, int], where int is the number of bins and array is the bin edges.

  • range (Union[list, tuple], optional) – has shape (2, 2), the leftmost and rightmost edges of the bins along each dimension (if not specified explicitly in the bins parameters): [[xmin, xmax], [ymin, ymax]]. All values outside of this range will be considered outliers and not tallied in the histogram.

  • weights (Union[list, tuple, Tensor], optional) – An array with shape (N,) of values w_i weighing each sample (x_i, y_i).

  • density (boolean, optional) – If False, the default, returns the number of samples in each bin. If True, returns the probability density function at the bin, bin_count / sample_count / bin_volume.


(Tensor, Tensor, Tensor), the values of the bi-directional histogram and the bin edges along the first and second dimensions.


ValueError – if range does not have the same size as the number of samples.

Supported Platforms:

Ascend GPU CPU


>>> from mindspore import numpy as np
>>> x = np.arange(5)
>>> y = np.arange(2, 7)
>>> print(np.histogram2d(x, y, bins=(2, 3)))
(Tensor(shape=[2, 3], dtype=Float32, value=
[[ 2.00000000e+00,  0.00000000e+00,  0.00000000e+00],
[ 0.00000000e+00,  1.00000000e+00,  2.00000000e+00]]),
Tensor(shape=[3], dtype=Float32, value= [ 0.00000000e+00,  2.00000000e+00,  4.00000000e+00]),
Tensor(shape=[4], dtype=Float32, value=
[ 2.00000000e+00,  3.33333349e+00,  4.66666698e+00,  6.00000000e+00]))