Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib hist while ignoring a particular no data value

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?

like image 237
Kurt Schwehr Avatar asked Nov 07 '11 16:11

Kurt Schwehr


People also ask

How do you plot a normalized histogram in Python?

To normalize a histogram in Python, we can use hist() method. In normalized bar, the area underneath the plot should be 1.

What does bins mean in PLT hist?

It is a type of bar graph. To construct a histogram, the first step is to “bin” the range of values — that is, divide the entire range of values into a series of intervals — and then count how many values fall into each interval. The bins are usually specified as consecutive, non-overlapping intervals of a variable.


1 Answers

You can use a boolean array to select the required indices:

selected_values = (e_data > (no_data + eps)) & (e_data < (no_data - eps))
pyplot.hist(e_data[selected_values])

(e_data > (no_data + eps)) will create an array of np.bool with the same shape as e_data, set to True at 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_data is just a convention, I would set those values to numpy.nan instead, and use e_data[numpy.isfinite(e_data)].

like image 199
Bruno Avatar answered Oct 18 '22 15:10

Bruno