如何在一系列箱线图中的箱线图旁边显示数值平均值和标准值?

2023-12-24

我试图在多个箱形图中的箱形图旁边显示平均值和标准差的值。当我尝试时什么也没有显示。

#Boxplot 3

data3 =np.array([[ 4.38,  3.27,  6.07],
   [ 4.35,  3.51,  6.14],
   [ 4.09,  3.33,  5.92],
   [ 4.9 ,  3.97,  5.02],
   [ 4.56,  3.5 ,  4.5 ],
   [ 4.78,  3.95,  4.58]])

fig3 = plt.figure(3)

ax3 = fig3.add_subplot(111)
ax3.boxplot(data3, showmeans=True)

ax3.set_title('Serve - Data Location and Variance 1',fontsize=15,fontweight='bold', y=1.06)
ax3.set_ylabel('Velocity [m/s]',fontsize=11.5)
ax3.set_xlabel('Parameter',fontsize=11.5)
ax3.set_xticklabels(['V_in', 'V_out', 'V_bat'],style='italic')
ax3.get_xaxis().tick_bottom()
ax3.get_yaxis().tick_left()


m1=data3.mean(axis=0) #Mean values 
mL1 = [str(np.round(s, 2)) for s in m1]

st1=data3.std(axis=0) #Standard deviation values 
sT1=[str(np.round(s, 2)) for s in st1]

ind=0
for i in range (len(ax3.get_xticklabels())):
ax3.text(i, m1[ind]+1, mL1[ind], horizontalalignment='center',  color='w', weight='semibold')
ind+=1

The last four lines need to be corrected as the other code is working fine (see picture) enter image description here


The boxplot方法返回一个字典,其中包含箱线图的各个部分(胡须、上限、盒子、中位数、飞行物、均值)。您可以使用它们在图中的不同位置添加注释。下面我在中线右侧添加了平均值和标准差值:

阅读本文了解更多详情在箱线图中叠加中位数/方差的数值 https://stackoverflow.com/questions/18861075/overlaying-the-numeric-value-of-median-variance-in-boxplots

m1 = data3.mean(axis=0)
st1 = data3.std(axis=0)

fig, ax = plt.subplots()
bp = ax.boxplot(data3, showmeans=True)

for i, line in enumerate(bp['medians']):
    x, y = line.get_xydata()[1]
    text = ' μ={:.2f}\n σ={:.2f}'.format(m1[i], st1[i])
    ax.annotate(text, xy=(x, y))

哪个情节

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在一系列箱线图中的箱线图旁边显示数值平均值和标准值? 的相关文章

随机推荐