Python pylab直方图摆脱nan
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19090070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pylab histogram get rid of nan
提问by usethedeathstar
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.
当我的某些数据包含“非数字”值时,我在制作直方图时遇到问题。我可以通过使用nan_to_num
numpy来消除错误,但是我得到了很多零值,这也弄乱了直方图。
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).
所以这个想法是制作另一个数组,其中所有的 nan 值都消失了,或者只是以某种方式在直方图中掩盖它们(最好使用一些内置方法)。
采纳答案by jabaldonedo
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:
使用 删除np.nan
数组中的值A[~np.isnan(A)]
,这将选择A
值不是 的所有条目nan
,因此在计算直方图时将排除它们。以下是如何使用它的示例:
>>> 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()