pylab histogram get rid of nan

末鹿安然 提交于 2019-11-29 17:07:07

问题


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).


回答1:


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()



来源:https://stackoverflow.com/questions/19090070/pylab-histogram-get-rid-of-nan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!