0

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 Answers1

6

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

Community
  • 1
  • 1
MattQ
  • 76
  • 3