9951 explained code solutions for 126 technologies


python-numpyHow to build histogram for 2-dimensional array


import numpy as np

x = np.array([5,6,6])
y = np.array([1,2,2])
hres = np.histogram2d(x, y, 3)ctrl + c
import numpy as np

load Numpy module for Python

np.array

declare Numpy array

.histogram2d

build 2-dimensional histogram

x, y

first and second dimension

3

number of bins to automatically create

hres

will contain calculated histogram as a list of [histogram, bin1, bin2]


Usage example

import numpy as np

x = np.array([5,6,6])
y = np.array([1,2,2])
hres = np.histogram2d(x, y, 3)

print(hres)
output
(array([[1., 0., 0.],
       [0., 0., 0.],
       [0., 0., 2.]]), array([5.        , 5.33333333, 5.66666667, 6.        ]), array([1.        , 1.33333333, 1.66666667, 2.        ]))