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

标签 python pandas text label boxplot

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

#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

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

最佳答案

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

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

哪些地 block

enter image description here

关于python - 如何在一系列箱形图中的箱形图旁边显示数值平均值和标准值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58066009/

相关文章:

python - 函数闭包与可调用类

python - 创建由另一列 pandas 分组的一列的排列

放大/缩小时换行的 HTML 元素

python - 高效创建具有重复结构的 NumPy 数组

python - 基于 yield 的协程是真正的协程吗?

python - 使用不同的 DataFrame 更改 pandas DataFrame 切片中的值

delphi - 在Delphi中显示多行文本的组件

text - 链接是图片时如何指定链接文字?

python - 双星后跟变量名在python中是什么意思?

python - 我在 python 中使用 Pandas 来读取 csv,如何传递工作表名称来读取特定工作表