pylab直方图摆脱nan [英] pylab histogram get rid of nan

查看:76
本文介绍了pylab直方图摆脱nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的某些数据包含非数字"值时,我在制作直方图时遇到问题.我可以使用numpy中的nan_to_num来消除该错误,但是我得到的很多零值也弄乱了直方图.

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

因此,想法是制作另一个所有nan值都消失了的数组,或者只是以某种方式(最好使用某些内置方法)将它们掩盖在直方图中.

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

推荐答案

使用A[~np.isnan(A)]从数组中删除np.nan值,这将选择A中所有不是nan值的条目,因此它们将在计算直方图时将其排除在外.这是一个使用方法的示例:

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

这篇关于pylab直方图摆脱nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆