如何在一系列箱形图的箱形图旁边显示数值均值和标准差值? [英] How to display numeric mean and std values next to a box plot in a series of box plots?

查看:224
本文介绍了如何在一系列箱形图的箱形图旁边显示数值均值和标准差值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在多个箱形图的绘图中,在箱形图旁边显示平均值和标准偏差的值.我尝试时什么也没显示.

  #Boxplot 3data3 = 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]])无花果3 = plt.figure(3)ax3 = fig3.add_subplot(111)ax3.boxplot(data3,showmeans = True)ax3.set_title('Serve-数据位置和方差1',fontsize = 15,fontweight ='bold',y = 1.06)ax3.set_ylabel('速度[m/s]',字体大小= 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)#平均值mL1 = [str(np.round(s,2))对于m中的s]st1 = data3.std(axis = 0)#标准偏差值sT1 = [str(np.round(s,2))对于st1中的s]ind = 0对于我在范围内(len(ax3.get_xticklabels())):ax3.text(i,m1 [ind] +1,mL1 [ind],horizo​​ntalalignment ='center',color ='w',weight ='semibold')ind + = 1 

由于其他代码正常工作,因此需要更正最后四行(参见图片)

解决方案

boxplot 方法返回一个字典,其中包含箱形图的各个部分(晶须,大写字母,大写字母,盒子,中位数,传单,均值).您可以使用它们在图中的不同位置添加注释.下面,我在中线右侧添加了平均值和标准偏差值:

有关详细信息,请阅读此内容

I'm trying to display the value of mean and standard deviation next to my box plots in a plot of multiple box plots. Nothing is displayed when I try.

#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)

解决方案

The boxplot method returns a dictionary that includes parts of the boxplot (whiskers, caps, boxes, medians, fliers, means). You can use these to add annotation at various location within the plot. Below I added mean and standard deviation values to the right of the median line:

Read this for more details 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))

which plots

这篇关于如何在一系列箱形图的箱形图旁边显示数值均值和标准差值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆