本文介绍了在matplotlib中不绘制“零"或将零更改为“无" [Python]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,我想将数据中的所有零转换为None(因为我不想在matplotlib中绘制数据).但是,该代码无法正常工作,并且仍在打印0.

I have the code below and I would like to convert all zero's in the data to None's (as I do not want to plot the data here in matplotlib). However, the code is notworking and 0. is still being printed

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

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

Can anyone think of a solution to this. Or just an answer for how I can transfer all 0 to None. Or just not plot the zero values in Matplotlib.

推荐答案

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

给您

[3, 5, nan, 3, 5, 1, 4, nan, 9]

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

Matplotlib won't plot nan (not a number) values.

这篇关于在matplotlib中不绘制“零"或将零更改为“无" [Python]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:36
查看更多