简体   繁体   English

Python matplotlib错误栏问题

[英]Python matplotlib errorbar issue

Given these numpy arrays 鉴于这些numpy数组

x = [0 1 2 3 4 5 6 7 8 9]
y = [[ 0.        ]
     [-0.02083473]
     [ 0.08819923]
     [ 0.9454764 ]
     [ 0.80604627]
     [ 0.82189822]
     [ 0.73613942]
     [ 0.64519742]
     [ 0.56973868]
     [ 0.612912  ]]
c = [[ 0.          0.        ]
     [-0.09127286  0.04960341]
     [-0.00300709  0.17940555]
     [ 0.82319693  1.06775586]
     [ 0.74512774  0.8669648 ]
     [ 0.75177669  0.89201975]
     [ 0.63606087  0.83621797]
     [ 0.57786173  0.7125331 ]
     [ 0.46722312  0.67225423]
     [ 0.54951714  0.67630685]]

I want to plot the graph of x,y , with error bars using the values in c. 我想使用c中的值绘制x,y的图形和误差线。 I tried 我试过了

plt.errorbar(x, y, yerr=c)

But the interpreter is giving me this error: 但是翻译给了我这个错误:

File "C:\Python\32\lib\site-packages\matplotlib\axes.py", line 3846, in vlines
  for thisx, (thisymin, thisymax) in zip(x,Y)]
File "C:\Python\32\lib\site-packages\matplotlib\axes.py", line 3846, in <listcomp>
  for thisx, (thisymin, thisymax) in zip(x,Y)]
ValueError: too many values to unpack (expected 2)

The value of x in zip is zipx的值是

[0 1 2 3 4 5 6 7 8 9]

and the value of Y in zip is zipY值是

[[[ 0.          0.        ]
  [ 0.07043814 -0.11210759]
  [ 0.09120632  0.08519214]
  [ 0.12227947  1.76867333]
  [ 0.06091853  1.55117401]
  [ 0.07012153  1.57367491]
  [ 0.10007855  1.3722003 ]
  [ 0.06733568  1.22305915]
  [ 0.10251555  1.0369618 ]
  [ 0.06339486  1.16242914]]

 [[ 0.          0.        ]
  [-0.07043814  0.02876869]
  [-0.09120632  0.26760478]
  [-0.12227947  2.01323226]
  [-0.06091853  1.67301107]
  [-0.07012153  1.71391797]
  [-0.10007855  1.57235739]
  [-0.06733568  1.35773052]
  [-0.10251555  1.2419929 ]
  [-0.06339486  1.28921885]]]

I've read around and it looks like my code should be correct (a stupid assumption, but I can't find evidence to the contrary... yet), but it looks like errorbar doesn't like the 2d array. 我已经阅读了,看起来我的代码应该是正确的(一个愚蠢的假设,但我找不到相反的证据......),但看起来像errorbar不喜欢2d数组。 The documentation says that yerr can be a 2d array, with the first column being the min error and the second being the max. 文档说yerr可以是2d数组,第一列是最小错误,第二列是最大值。

What is it that I'm doing wrong here? 我在这里做错了什么?

There were some problems with the code which I corrected below and so it works with no problem. 我在下面更正了代码的一些问题,因此它没有问题。

import numpy
import pylab

arr = numpy.asarray

x = arr([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])   # put comma between numbers
y = arr([[ 0.        ],                   # make it vector
     [-0.02083473],
     [ 0.08819923],
     [ 0.9454764 ],
     [ 0.80604627],
     [ 0.82189822],
     [ 0.73613942],
     [ 0.64519742],
     [ 0.56973868],
     [ 0.612912  ]]).flatten()
c = arr([[ 0.        ,  0.        ],
     [-0.09127286,  0.04960341],
     [-0.00300709,  0.17940555],
     [ 0.82319693,  1.06775586],
     [ 0.74512774,  0.8669648 ],
     [ 0.75177669,  0.89201975],
     [ 0.63606087,  0.83621797],
     [ 0.57786173,  0.7125331 ],
     [ 0.46722312,  0.67225423],
     [ 0.54951714,  0.67630685]]).T      # transpose
pylab.errorbar(x, y, yerr=c)
pylab.show()

and the result: 结果:

在此输入图像描述

Good luck. 祝好运。

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

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