2

I am confused by the order of arguments to plt.errorbar

I have the following code in my Python script

plt.errorbar(0.0, 1.0, 0.1, 10.0)

I would assume the order of arguments is: x_data, y_data, x_error, y_error

This agrees with: http://matplotlib.org/1.2.1/examples/pylab_examples/errorbar_demo.html

However, when I run this code I get the following output:

Screenshot

Clearly, x=0.0, y=1.0, x_err=10.0, y_err=0.1

So the x_error and y_error arguments are swapped!

My question is why? Is the documentation wrong? I am so confused!

1 Answer 1

5

If you don't use keywords, the order of the arguments is x, y, yerr, xerr.

From the docs:

Call signature:

errorbar(x, y, yerr=None, xerr=None,
     fmt='', ecolor=None, elinewidth=None, capsize=None,
     barsabove=False, lolims=False, uplims=False,
     xlolims=False, xuplims=False, errorevery=1,
     capthick=None)

To avoid this, you can use the keywords to make sure you are giving the correct value to the correct argument; then the order doesn't matter. Note that this is how the example you linked to does it too.

So, for your example, you would want to use:

plt.errorbar(0.0, 1.0, xerr=0.1, yerr=10.0)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - I didn't know I could re-order arguments in Python!
@user3728501 you have to name them if you want to reorder them. (In my opinion, you should always name them)

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.