2017-07-03 25 views
0

如何從ax.bar對象中檢索yerr值? 條形圖使用單個線條創建,ax.bar()的每個參數都是一個集合,包括yerr值。從matplotlib中的小節對象中檢索yerr值

bar_list = ax.bar(x_value_list, y_value_list, color=color_list, 
        tick_label=columns, yerr=confid_95_list, align='center') 

後來,我希望能夠檢索圖表中每個單獨條的y值和yerr值。 我遍歷bar_list集合,我可以檢索y值,但我不知道如何檢索yerr值。

獲取Y值如下:

for bar in bar_list: 
    y_val = bar.get_height() 

我怎樣才能獲得yerr?有沒有像bar.get_yerr()方法? (這不是bar.get_yerr()) 我想能夠:

for bar in bar_list: 
    y_err = bar.get_yerr() 

回答

1

注意,在上面的例子中confid_95_list已經錯誤的列表。所以沒有必要從劇情中獲得它們。

要回答這個問題:在行for bar in bar_listbar是一個Rectangle,因此沒有與它關聯的錯誤欄。

但是bar_list是一個條形容器,其屬性爲errorbar,其中包含錯誤欄創建的返回。然後您可以獲得線路集合的各個部分。每條線從yminus = y - y_erroryplus = y + y_error;線路集合僅存儲點yminus,yplus。舉個例子:

means = (20, 35) 
std = (2, 4) 
ind = np.arange(len(means)) 

p = plt.bar(ind, means, width=0.35, color='#d62728', yerr=std) 

lc = [i for i in p.errorbar.get_children() if i is not None][0] 
for yerr in lc.get_segments(): 
    print (yerr[:,1]) # print start and end point 
    print (yerr[1,1]- yerr[:,1].mean()) # print error 

將打印

[ 18. 22.] 
2.0 
[ 31. 39.] 
4.0 

所以這很適合symmectric errorbars。對於不對稱誤差線,您還需要考慮這一點。

means = (20, 35) 
std = [(2,4),(5,3)] 
ind = np.arange(len(means)) 

p = plt.bar(ind, means, width=0.35, color='#d62728', yerr=std) 

lc = [i for i in p.errorbar.get_children() if i is not None][0] 
for point, yerr in zip(p, lc.get_segments()): 
    print (yerr[:,1]) # print start and end point 
    print (yerr[:,1]- point.get_height()) # print error 

將打印

[ 18. 25.] 
[-2. 5.] 
[ 31. 38.] 
[-4. 3.] 

這似乎過於複雜結束,因爲你只檢索您最初投入,meansstd值,你可以簡單地使用任何你想要的值做。