I’ve got a 2D numpy array with 1.0e6 as the no data value. I’d like to generate a histogram of the data and while I’ve succeeded this can’t be the best way to do it.
from matplotlib import pyplot
import sys
eps = sys.float_info.epsilon
no_data = 1.0e6
e_data = elevation.reshape(elevation.size)
e_data_clean = [ ]
for i in xrange(len(e_data)):
val = e_data[i]
# floating point equality check for val aprox not equal no_data
if val > no_data + eps and val < no_data - eps:
e_data_clean.append(val)
pyplot.hist(e_data_clean, bins=100)
It seems like there should be a clean (and much faster one liner for this). Is there?
You can use a boolean array to select the required indices:
(e_data > (no_data + eps))will create an array ofnp.boolwith the same shape ase_data, set toTrueat a given index if and only if the value at that index is greater than(no_data + eps).&is the element-wise and operator to satisfy both conditions.Alternatively, if
no_datais just a convention, I would set those values tonumpy.naninstead, and usee_data[numpy.isfinite(e_data)].