从 matplotlib 中的 bar 对象中检索 yerr 值
Retrieve yerr value from bar object in matplotlib
如何从 ax.bar 对象中检索 yerr 值?
条形图是用一条线创建的,ax.bar()的每个参数都是一个集合,包括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()
请注意,在上面的示例中 confid_95_list
是 已经是错误列表。所以没有必要从情节中获取它们。
回答问题:在行 for bar in bar_list
中,bar
是一个 Rectangle
,因此没有与之关联的错误栏。
然而bar_list
是一个带有属性errorbar
的bar容器,它包含了errorbar创建的return。然后,您可以获得行集合的各个段。每行从 yminus = y - y_error
到 yplus = 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
所以这适用于对称误差线。对于不对称误差条,您还需要考虑点本身。
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.]
最后这似乎不必要地复杂,因为您只检索最初输入的值,means
和 std
,您可以简单地使用这些值来做任何您想做的事情。
如何从 ax.bar 对象中检索 yerr 值? 条形图是用一条线创建的,ax.bar()的每个参数都是一个集合,包括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()
请注意,在上面的示例中 confid_95_list
是 已经是错误列表。所以没有必要从情节中获取它们。
回答问题:在行 for bar in bar_list
中,bar
是一个 Rectangle
,因此没有与之关联的错误栏。
然而bar_list
是一个带有属性errorbar
的bar容器,它包含了errorbar创建的return。然后,您可以获得行集合的各个段。每行从 yminus = y - y_error
到 yplus = 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
所以这适用于对称误差线。对于不对称误差条,您还需要考虑点本身。
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.]
最后这似乎不必要地复杂,因为您只检索最初输入的值,means
和 std
,您可以简单地使用这些值来做任何您想做的事情。