简体   繁体   English

python:如何将p值显着性添加到barplot

[英]python: How to add p values signifance to barplot

Below i have a code for the barplot, I would also like to show the Pvalue significane for these plots. 在下面,我有条形图的代码,我还要显示这些图的Pvalue含义。 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 导致 在此处输入图片说明

Here is another solution which puts the p-values to the plot's legend. 这是将p值添加到绘图图例的另一种解决方案。 For my eyes, this is more pleasant compared to plotting the p-values over the bars. 对我而言,与在条形图上绘制p值相比,这更令人愉快。

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()
cont = ax.bar(x_labels, X, width=0.4, align='center', yerr=error)

for i, art in enumerate(cont):
    art.set_color('C{}'.format(i))

ax.legend(cont.patches, [r'$p={:.6f}$'.format(pv) for pv in pvalue])

图例中带有p值的条形图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM