不在matplotlib中绘制'零'或将零更改为无[Python]

Ash*_*ton 10 python numpy matplotlib zero

我有下面的代码,我想将数据中的所有零转换为None's(因为我不想在matplotlib中绘制数据).但是,代码不起作用,0.仍在打印中

sd_rel_track_sum=np.sum(sd_rel_track, axis=1)
for i in sd_rel_track_sum:
   print i
   if i==0:
       i=None

return sd_rel_track_sum
Run Code Online (Sandbox Code Playgroud)

任何人都可以想到解决这个问题.或者只是我可以将所有0转移到的答案None.或者只是不在Matplotlib中绘制零值.

Dan*_*iel 21

为什么不使用numpy呢?

>>> values = np.array([3, 5, 0, 3, 5, 1, 4, 0, 9], dtype=np.double)
>>> values[ values==0 ] = np.nan
>>> values
array([  3.,   5.,  nan,   3.,   5.,   1.,   4.,  nan,   9.])
Run Code Online (Sandbox Code Playgroud)

应该注意,值不能是整数类型数组.


tam*_*gal 17

values = [3, 5, 0, 3, 5, 1, 4, 0, 9]

def zero_to_nan(values):
    """Replace every 0 with 'nan' and return a copy."""
    return [float('nan') if x==0 else x for x in values]

print(zero_to_nan(values))
Run Code Online (Sandbox Code Playgroud)

给你:

[3, 5, nan, 3, 5, 1, 4, nan, 9]
Run Code Online (Sandbox Code Playgroud)

Matplotlib不会绘制nan(不是数字)值.