You could simply compare the hist_values
with the threshold
, this would give you a mask as an array of bool
which can be used in slicing, e.g.:
import numpy as np
# prepare random input
arr1 = np.random.randint(0, 100, 1000)
arr2 = np.random.randint(0, 100, 1000)
# compute 2D histogram
hist_values, hist_x, hist_y = np.histogram2d(arr1, arr2)
mask = hist_values > threshold # the array of `bool`
hist_values[mask] # only the values above `threshold`
Of course, the values are then collected in a flattened array.
Alternatively, you could also use mask
to instantiate a masked-array object (using numpy.ma
, see docs for more info on it).
If you are after the coordinates at which this is happening, you should use numpy.where()
.
# i0 and i1 contain the indices in the 0 and 1 dimensions respectively
i0, i1 = np.where(hist_values > threshold)
# e.g. this will give you the first value satisfying your condition
hist_values[i0[0], i1[0]]
For the corresponding values of hist_x
and hist_y
you should note that these are the boundaries of the bins, and not, for example, the mid-values, therefore you could resort to the lower or upper bound of it.
# lower edges of `hist_x` and `hist_y` respectively...
hist_x[i0]
hist_y[i1]
# ... and upper edges
hist_x[i0 + 1]
hist_y[i1 + 1]