繁体   English   中英

matplotlib使用索引在for循环中绘制不对称错误栏

[英]matplotlib plot asymmetric errorbars in for loop with indexes

我正在尝试使用带有不对称错误栏的plt.errorbar用for循环绘制一些数组。 我知道在此示例中,使用没有索引的数组会更容易,但是我想在索引i上有某些条件的情况下,在其他代码中使用plt.errorbar中的索引。

这是示例代码:

   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()

输出错误为:

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

另外,如果我尝试使用对称错误栏( plt.errorbar(x[i], y[i], xerr=xallel[i]) ),它也可以工作。 所以我不明白问题出在哪里。

我需要索引,因为我需要在循环顶部的索引i上添加条件。 例如:

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')

我该如何解决? 我正在使用anaconda Python 3.6。

您的xerr数组不是2xN,应该是2x1:

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

您也可以避免循环matplotlib通常支持一次绘制多个值。

plt.errorbar(x, y, xerr=[xel,xeu], fmt='.')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM