Python matplotlib - errorbar无序列值

时间:2014-06-11 12:41:48

标签: python matplotlib

我试图用错误条绘制一个系列。该系列可能包含无值。不使用错误时 - 系列绘制没有错误。当尝试使用错误栏进行绘图时 - 我收到此错误:

我的代码是:

x = [10.4, 11.12,11.3,None, 10.2,11.3]
y = [0.3, 1.2, 0.7, None, 1.1, 0.4]
y_err = [0.01, 0.04, 0.07, None, 0.01, 0.05] 

plt.plot(x,y, 'o', color='r') # this one works. I get a plot with 5 points. The null point is skipped
plt.errorbar(x,y,yerr=y_err) # this one doesn't work

我得到的错误是:

 TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'

有没有办法跳过系列中的空值?

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试使用NaN而不是“无”。

x = [10.4, 11.12,11.3,float('NaN'), 10.2,11.3]
y = [0.3, 1.2, 0.7, float('NaN'), 1.1, 0.4]
y_err = [0.01, 0.04, 0.07, float('NaN'), 0.01, 0.05] 
plt.plot(x,y, 'o', color='r')
plt.errorbar(x,y,yerr=y_err)

Assigning a variable NaN in python without numpy