Search code examples
pythonmatplotliberrorbar

python ValueError safezip: different length of data and errordata?


I am trying to plot some data with errorbars in python but get constantly the following error message:

  (21,) (21,) (2, 21) (21,)
  Traceback (most recent call last):
  File "q_profil.py", line 104, in <module>
ax.errorbar(qmean,np.arange(-5,5.5,0.5),'.-') #np.array([qstdev,qstdev]
 File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 5547, in errorbar
in cbook.safezip(y,yerr[0])]
 File "/usr/lib64/python2.7/site-packages/matplotlib/cbook.py", line 1225, in safezip
raise ValueError(_safezip_msg % (Nx, i + 1, len(arg)))
ValueError: In safezip, len(args[0])=21 but len(args[1])=1

The code:

  qmean = np.mean(qn,axis=1)*1e03
  qstdev = np.std(qn,axis=1)*1e03
  xerr=[qstdev,qstdev]
  print qmean.shape, qstdev.shape, np.shape(xerr),np.arange(-5,5.5,0.5).shape

  fig = plt.figure()
  ax = fig.add_subplot(111)
  ax.errorbar(qmean,np.arange(-5,5.5,0.5),'.-',xerr=[qstdev,qstdev])

I also tried already

  ax.errorbar(qmean,np.arange(-5,5.5,0.5),'.-',xerr=qstdev)

and

  ax.errorbar(qmean,np.arange(-5,5.5,0.5),'.-',xerr=np.array([qstdev,qstdev]))

with always the same error. Even xerr=None got me this.

As you can see, I printed the shapes of qmean (my data), qstdev (the size of the error-data array) and the range-array (arange from -5 to 5.5) which is the first line in the first Code-Block.

For

  ax.errorbar(qmean,np.arange(-5,5.5,0.5),'.-',xerr=xerr)

I got the error with len(args[1])=2

Anybody any advice what to try? Didn't find any useful hint on the web. Usually users solved their problems with exactly the same code, I am using. I am wondering about the error because shapes of my data seem right.


Solution

  • Got it! "errorbar" does not accept the format of the plot as a non-keyword argument

    ax.errorbar(qmean, np.arange(-5, 5.5, 0.5), '.-')
    

    but instead wants a keyword argument

    ax.errorbar(qmean, np.arange(-5,5.5,0.5), xerr=[qstdev,qstdev], fmt='.-')