Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pylab histogram get rid of nan

I have a problem with making a histogram when some of my data contains "not a number" values. I can get rid of the error by using nan_to_num from numpy, but than i get a lot of zero values which mess up the histogram as well.

pylab.figure()
pylab.hist(numpy.nan_to_num(A))
pylab.show()

So the idea would be to make another array in which all the nan values are gone, or to just mask them in the histogram in some way (preferrably with some builtin method).

like image 313
usethedeathstar Avatar asked Sep 30 '13 08:09

usethedeathstar


People also ask

How do you remove NaN values from an array?

How to drop all missing values from a numpy array? Droping the missing values or nan values can be done by using the function "numpy. isnan()" it will give us the indexes which are having nan values and when combined with other function which is "numpy. logical_not()" where the boolean values will be reversed.

What is bins in NP histogram?

A bin is range that represents the width of a single bar of the histogram along the X-axis. You could also call this the interval.


1 Answers

Remove np.nan values from your array using A[~np.isnan(A)], this will select all entries in A which values are not nan, so they will be excluded when calculating histogram. Here is an example of how to use it:

>>> import numpy as np
>>> import pylab

>>> A = np.array([1,np.nan, 3,5,1,2,5,2,4,1,2,np.nan,2,1,np.nan,2,np.nan,1,2])

>>> pylab.figure()
>>> pylab.hist(A[~np.isnan(A)])
>>> pylab.show()

enter image description here

like image 94
jabaldonedo Avatar answered Sep 19 '22 06:09

jabaldonedo