插入错误栏时出现AssertionError

我正在尝试将误差线插入到我的barplot中,但是我收到了一个我不太了解的错误。我想解决方案并不那么复杂,但是在论坛中寻找类似问题时,我似乎也找不到。

Curtailment_Herrenhof_A     = 12210
Curtailment_Herrenhof_B     = 22170
Curtailment_Herrenhof_data  = 8896
std = 2000



plt.figure
plt.bar('eff avg',Curtailment_Herrenhof_A,width = 0.5,label='Method A')
plt.bar('none',Curtailment_Herrenhof_B,label='Method B')
plt.bar('wind avg',Curtailment_Herrenhof_data,label='Measured data')
plt.errorbar('eff avg',yerr= std,marker='o',fmt='') 
plt.ylabel('Durtaion of curtailment measures [min]',fontsize=12)
plt.legend(bbox_to_anchor=(-0.018,1.2),loc='upper left',ncol=3)
plt.xticks([])
plt.tight_layout()
plt.savefig('Curtailment_Wirdum_time.png')
plt.show()

出现错误:

  File "C:/Users/Elias/Anaconda3/Scripts/Masterarbeit/plotten.py",line 213,in <module>
    plt.errorbar('eff avg',fmt='')

  File "C:\Users\Elias\Anaconda3\lib\site-packages\matplotlib\pyplot.py",line 2991,in errorbar
    **kwargs)

  File "C:\Users\Elias\Anaconda3\lib\site-packages\matplotlib\__init__.py",line 1867,in inner
    return func(ax,*args,**kwargs)

  File "C:\Users\Elias\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py",line 3324,in errorbar
    xo,_ = xywhere(x,lower,noylims & everymask)

  File "C:\Users\Elias\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py",line 3225,in xywhere
    assert len(xs) == len(ys)

AssertionError

因此似乎确实存在某种尺寸错误,我确实没有得到?我只是想绘制整数,所以看不到问题。

也许有人可以指出我正确的方向?

bestd 回答:插入错误栏时出现AssertionError

为什么到处传递字符串作为第一个参数?例如,pyplot.bar期望其第一个参数是“标量序列”,而不是字符串。 Same thing for errorbar.

由于以下原因,您会收到此特定错误:

plt.errorbar('eff avg',Curtailment_Herrenhof_A,...)

您正在尝试针对单个数字'eff avg'绘制字符串(!)Curtailment_Herrenhof_A,因此,在matplotlibxs == 'eff avg'ys == [12210]内部。当然,len('eff avg') != len([12210]),所以您会得到错误。

此外,plt.figure不会执行任何操作。 It's a function,并且应该调用以实际执行操作。

,

问题在于字符串(例如"abcde")可以解释为字符列表(["a","b",...])。因此,它也有长度。但是,您想要的是将整个字符串视为列表的单个元素,以便能够针对另一个值来绘制它。因此,您不想将plt.bar("my string",100)放在一个带有单个元素的列表中。

plt.bar(["my string"],100)

您的完整代码:

Curtailment_Herrenhof_A     = 12210
Curtailment_Herrenhof_B     = 22170
Curtailment_Herrenhof_data  = 8896
std = 2000

import matplotlib.pyplot as plt
plt.figure()
plt.bar(['eff avg'],width = 0.5,label='Method A')
plt.bar(['none'],Curtailment_Herrenhof_B,label='Method B')
plt.bar(['wind avg'],Curtailment_Herrenhof_data,label='Measured data')
plt.errorbar(['eff avg'],yerr= std,marker='o',fmt='') 
plt.ylabel('Durtaion of curtailment measures [min]',fontsize=12)
plt.legend(bbox_to_anchor=(-0.018,1.2),loc='upper left',ncol=3)

plt.tight_layout()
plt.savefig('Curtailment_Wirdum_time.png')
plt.show()

enter image description here

本文链接:https://www.f2er.com/3128287.html

大家都在问