Search code examples
pythonmatplotliberrorbar

python - strange error when plotting errorbars


I'm trying to combine 3 datasets in one plot. Each dataset has it's own y and x error. I'm receiving this error message:

Traceback (most recent call last):
  File "SED_plot.py", line 310, in <module>
    plt.errorbar(x0, y0, xerr=x0err, linestyle='None', ecolor="black", label= "Channel Width")
  File "/Library/Python/2.7/site-packages/matplotlib-override/matplotlib/pyplot.py", line 2766, in errorbar
    errorevery=errorevery, capthick=capthick, **kwargs)
  File "/Library/Python/2.7/site-packages/matplotlib-override/matplotlib/axes/_axes.py", line 2749, in errorbar
    in cbook.safezip(x, xerr[0])]
  File "/Library/Python/2.7/site-packages/matplotlib-override/matplotlib/cbook.py", line 1479, in safezip
    raise ValueError(_safezip_msg % (Nx, i + 1, len(arg)))
ValueError: In safezip, len(args[0])=16 but len(args[1])=48

when I run this code:

x0, y0          = x_val_all[0:16], y_val_all[0:16]
x0err, y0err    = x_error_all[0:16], y_error_all[0:16]
x1, y1          = x_val_all[17:33], y_val_all[17:33]
x1err, y1err    = x_error_all[17:33], y_error_all[17:33]
x2, y2          = x_val_all[33:49], y_val_all[33:49]
x2err, y2err    = x_error_all[33:49], y_error_all[33:49]

plt.errorbar(x0, y0, xerr=x0err, linestyle='None', ecolor="black", label= "Channel Width")
plt.errorbar(x0, y0, yerr=y0err, linestyle='None', ecolor="black", label= "Standard Deviation")
plt.errorbar(x1, y1, xerr=x1err, yerr=y1err, ecolor="red")
plt.errorbar(x2, y2, xerr=x2err, yerr=y2err, ecolor="purple")
plt.show()

Could it be that list slicing isn't working in this case? All the x values and y values are in one list each (x_val_all, y_val_all respectively) and so are the corresponding errors.

Sample code to reproduce:

import matplotlib.pyplot as plt

y = range(0,21,1)
x = range(0,21,1)
y_err = [0.5]*21

x_low = [0.7]*21
x_upper = [1.4]*21
x_err = [x_low, x_upper]


plt.errorbar(x[0:7],y[0:7], xerr=x_err[0:7], yerr=y_err[0:7], linestyle="none", color="black")
plt.errorbar(x[8:15],y[8:15], xerr=x_err[8:15], yerr=y_err[8:15], linestyle="none", color="red")

plt.show()

Solution

  • Indexing x_err is the root cause of your error, as this is a list of two elements. My personal preference to fix this would be to use a list comprehension:

    import matplotlib.pyplot as plt
    
    y = range(0,21,1)
    x = range(0,21,1)
    y_err = [0.5]*21
    
    x_low = [0.7]*21
    x_upper = [1.4]*21
    x_err = [x_low, x_upper]
    
    plt.errorbar(x[0:7], y[0:7], xerr=[_x[0:7] for _x in x_err], yerr=y_err[0:7], linestyle="none", color="black")
    plt.errorbar(x[8:15], y[8:15], xerr=[_x[8:15] for _x in x_err], yerr=y_err[8:15], linestyle="none", color="red")
    
    plt.show()
    

    (Note the use of _x within the list comprehension - list comprehension leaks into the local scope in Python 2.7, which would overwrite the earlier x variable if we used x as the variable within the comprehension.)

    You could also do:

    plt.errorbar(x[0:7], y[0:7], xerr=[x_err[0][0:7], x_err[1][0:7]], yerr=y_err[0:7], linestyle="none", color="black")
    plt.errorbar(x[8:15], y[8:15], xerr=[x_err[0][8:15], x_err[1][8:15]], yerr=y_err[8:15], linestyle="none", color="red")
    

    although this is a little more verbose.