【问题标题】:How to display numeric mean and std values next to a box plot in a series of box plots?如何在一系列箱形图中的箱形图旁边显示数值平均值和标准值?
【发布时间】:2019-09-24 03:00:08
【问题描述】:

我正在尝试在多个箱形图中的箱形图旁边显示平均值和标准差的值。我尝试时没有显示任何内容。

#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

最后四行需要更正,因为其他代码工作正常(见图)

【问题讨论】:

    标签: python pandas text label boxplot


    【解决方案1】:

    boxplot 方法返回一个字典,其中包含箱线图的某些部分(须、大写、框、中值、传单、均值)。您可以使用它们在图中的不同位置添加注释。下面我在中线右侧添加了均值和标准差:

    阅读本文了解更多详情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))
    

    哪些地块

    【讨论】:

    • 谢谢!但是,我的绘图没有显示希腊字母,而是显示小矩形(好像它不能显示希腊字母)。有什么办法解决这个问题吗?
    • 你的 Matplotlib 库的版本是多少?我想2.0版本以后,默认字体支持数学字符、希腊字母等。
    • 我有一个旧版本,所以我更新了 Python 和所有的包。现在它起作用了。非常感谢!
    • 嗨@stahamtan - 你也可以帮忙[stackoverflow.com/questions/58268651/…?
    • 非常感谢,为我节省了很多时间!
    猜你喜欢
    • 2015-06-28
    • 1970-01-01
    • 2019-04-02
    • 2012-08-31
    • 2011-01-30
    • 2011-04-19
    • 1970-01-01
    • 2015-04-12
    • 2021-01-21
    相关资源
    最近更新 更多