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!

4
  • Are nones coming at the same index for x,y, and y_err? Commented Jun 11, 2014 at 12:44
  • And how big are the series normally? If you can simply filter out all the None values before plotting this could be easily done. Question is: do you have time and memory to do this? Commented Jun 11, 2014 at 12:46
  • @Sleepyhead - The Nones are coming at the same Index. Commented Jun 11, 2014 at 12:51
  • @Alexander - Pre filtering may be time consuming, and I was hoping to avoid that... Commented Jun 11, 2014 at 12:52

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.