Python Matplotlib错误栏问题 [英] Python matplotlib errorbar issue
问题描述
给出这些numpy数组
Given these numpy arrays
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]]
我想使用c中的值绘制带有误差线的x,y图.我尝试过
I want to plot the graph of x,y , with error bars using the values in c. 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)
zip
中x
的值是
[0 1 2 3 4 5 6 7 8 9]
,zip
中Y
的值是
[[[ 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]]]
我已经阅读了一下,看起来我的代码应该是正确的(一个愚蠢的假设,但是我还没有找到相反的证据……),但是它看起来像errorbar
不喜欢2d数组.文档说yerr
可以是2d数组,第一列是最小值,第二列是最大值.
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. The documentation says that yerr
can be a 2d array, with the first column being the min error and the second being the max.
我在这里做错什么了?
推荐答案
我在下面纠正的代码存在一些问题,因此可以正常工作.
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()
和结果:
祝你好运.
这篇关于Python Matplotlib错误栏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!