python:如何将p值显着性添加到条形图 [英] python: How to add p values signifance to barplot

查看:238
本文介绍了python:如何将p值显着性添加到条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我有一个条形图的代码,我还想显示这些图的 Pvalue 意义.有没有什么简单的方法可以表明这些条形的统计显着性

 将matplotlib.pyplot导入为pltX = [-0.9384815619939103、1.0755888058123153、0.061274066731665564、0.65064830688728]x_labels = ['A','B','C','D']错误= [0.23722952107696088、0.25505883348061764、0.26038015798295744、0.26073839861422]pvalue = [0.000076、0.000025、0.813956、0.012581]无花果,ax = plt.subplots()ax.bar(x_labels,X,width = 0.4,align ='center',yerr = error)plt.show()

解决方案

可以像

Below i have a code for the barplot, I would also like to show the Pvalue significane for these plots. Is there any easy way to indicate the statistical significance for these bars

import matplotlib.pyplot as plt

X= [-0.9384815619939103, 1.0755888058123153, 0.061274066731665564, 0.65064830688728]
x_labels = ['A' ,'B', 'C', 'D']

error = [0.23722952107696088, 0.25505883348061764, 0.26038015798295744, 0.26073839861422]
pvalue = [0.000076, 0.000025, 0.813956, 0.012581]

fig, ax = plt.subplots()
ax.bar(x_labels, X, width=0.4, align='center', yerr=error)
plt.show()

解决方案

It can be done like the way shown here with slight modification

import matplotlib.pyplot as plt   
X= [-0.9384815619939103, 1.0755888058123153, 0.061274066731665564,0.65064830688728]
x_labels = ['A' ,'B', 'C', 'D']
error = [0.23722952107696088, 0.25505883348061764, 0.26038015798295744, 0.26073839861422]
pvalue = [0.000076, 0.000025, 0.813956, 0.012581]

fig, ax = plt.subplots()
rects = ax.bar(x_labels, X, width=0.4, align = 'center', yerr=error)



def autolabel(rects,  pvalue, xpos='center',):
    """
    Attach a text label above each bar in *rects*, displaying its height.

    *xpos* indicates which side to place the text w.r.t. the center of
    the bar. It can be one of the following {'center', 'right', 'left'}.
    """

    xpos = xpos.lower()  # normalize the case of the parameter
    ha = {'center': 'center', 'right': 'left', 'left': 'right'}
    offset = {'center': 0.5, 'right': 0.57, 'left': 0.43}  # x_txt = x + w*off

    for i, rect in enumerate(rects):
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()*offset[xpos], 1.01*height,
                'p = {}'.format(pvalue[i]), ha=ha[xpos], va='bottom')
autolabel(rects, pvalue, "left")

plt.show()

which results in

这篇关于python:如何将p值显着性添加到条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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