Python matplotlib - errorbar None values in series

放肆的年华 提交于 2021-01-29 07:12:02

问题


I was trying to plot a series with error bars. The series may contain None values. When not using the errors - the series is plotted with no error. When trying to plot with the error bars - I get this error:

My code is:

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

The error I get is:

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

Is there any way to skip the null values in a series?

Thanks!


回答1:


Try using NaN rather than "None".

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



来源:https://stackoverflow.com/questions/24163237/python-matplotlib-errorbar-none-values-in-series

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!