0

I'm trying to plot some arrays with a for loop using plt.errorbar with asymmetric errorbars. I know that in this example it would be easier to use arrays without indexes, but I would like to use the indexes in plt.errorbar in other code with some conditions on the index i.

This is the example code:

   import numpy as np
   import matplotlib.pyplot as plt


    x=[ 0.007206,  0.043695,  0.372777,  0.464819,  0.337386,  0.249215,  0.395453,
      0.222331,  0.11715,   0.101464,  0.645596,  0.228634,  0.187252,  0.283026,
      0.596368,  0.019066,  0.300215,  0.174883,  0.331613,  0.175409,  0.858567, 0.895389]

    y=[  0.327811,  33.3177,     1.36996,   41.9717,     1.18497,    1.05182,    2.28229,
       0.424775,   1.11758,    4.5135,     2.70709,    1.26611,   10.8293,     4.92649,
      31.4483,     0.403496,   1.14471,    1.72301,   12.081,      0.501048,  13.6858,
       4.58709 ]

    xel=[ 0.034065,  0.096869,  0.046961,  0.13575,   0.086615,  0.070706,  0.068376,
      0.132277,  0.12079,   0.102303,  0.048192,  0.070823,  0.067665,  0.07266,
      0.093411,  0.040662,  0.089356,  0.098089,  0.137559,  0.146229,  0.038649,
      0.030372]
    xeu=[ 0.032612,  0.092047,  0.04424,   0.151329,  0.077828,  0.066373,  0.065701,
      0.123756,  0.09371,   0.086466,  0.047322,  0.069837,  0.070206,  0.058787,
      0.088777,  0.045837,  0.098174,  0.105,     0.148259,  0.14845,   0.133334,
      0.104611]

    for i in range(21):
        plt.errorbar(x[i], y[i], xerr=[xel[i],xeu[i]], fmt='.')

    plt.show()

The output error is:

ValueError: err must be [ scalar | N, Nx1 or 2xN array-like ]

Also If i try with symmetric errorbars (plt.errorbar(x[i], y[i], xerr=xallel[i])) it works. So I don't understand where problem is.

I need the indexes because I need to add a condition on the index i at the top of the loop. For example:

for i in range(21):
    if y[i]<1.0:
        plt.errorbar(x[i], y[i], xerr=[xel[i], xeu[i]], fmt='.', color='red')
    else:
        plt.errorbar(x[i], y[i], xerr=[xel[i], xeu[i]], fmt='.', color='b')

How can i fix that? I'm using anaconda Python 3.6.

1 Answer 1

1

Your xerr array is not 2xN, it should be 2x1:

xerr=[[xel[i]],[xeu[i]]]

You can avoid the loop as well matplotlib usually supports plotting multiple values at once.

plt.errorbar(x, y, xerr=[xel,xeu], fmt='.')
Sign up to request clarification or add additional context in comments.

5 Comments

Yes I know i can plot all data without indexes, but I need indexes. This is a simple example for a complex problem...
The code above that permits you to plot without indexes
I'm sorry but if I add a condition on the index, for example if y[i]<1.0, this solution plot all the points of the y array.
I'm sorry, I did not see the double square brackets. now it works, thank you very much!
@ImportanceOfBeingErnest there is no need to be rude. thanks also to you for your advice.

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.