statannotations.Annotator使用自定义P-value添加显著性差异

用seaborn绘制箱线图时,需要添加显著性差异。在查了一些资料后,了解到可以用Annotator添加注释

import seaborn as sns
import matplotlib.pyplot as plt
from statannotations.Annotator import Annotator

#绘制箱线图
ax = sns.boxplot(x='label', y=name, data=data)

#---------------添加显著性差异--------------------
pairs=[('A','B'),('A','C'),('B','C')]#设置比较的组
anno = Annotator(ax,pairs,x='label', y=name, data=data)
anno.configure(test='t-test_ind') #t检验
anno.apply_and_annotate() #加*

我的数据需要使用Kolmogorov–Smirnov (K-S)检验,而Annotator只支持t-test_ind, t-test_welch, t-test_paired, Mann-Whitney, Mann-Whitney-gt, Mann-Whitney-ls, Levene, Wilcoxon, Kruskal, Brunner-Munzel

翻看源码和tutorial,发现statannotations.Annotator支持三种功能:

- Add custom text annotations (`set_custom_annotations`)
- Format pvalues and add them to the plot (`set_pvalues`)
- Perform a statistical test and then add the results to the plot (`apply_test`)

之前使用的是第三种,因为我已经有数据的P-value了,所以可以直接使用第一种方式。

p_value=[0.003567,0.015769,0.000582]
anno = Annotator(ax,pairs,x='label', y=name, data=data)
anno.annotate_custom_annotations([str(i) for i in p_value])

需要注意的是,源码中要求:

text_annot_custom: List of strings to annotate for each `pair`

text_annot_custom就是代码里的[str(i) for i in p_value],需要是字符串列表,且和pairs一样长

此外,这个方法不能自动添加*号,只能添加文字,因此可以自己写一个数字和*转换的函数

def convert_pvalue_to_asterisks(pvalue):
    if pvalue <= 0.0001:
        return "****"
    elif pvalue <= 0.001:
        return "***"
    elif pvalue <= 0.01:
        return "**"
    elif pvalue <= 0.05:
        return "*"
    return "ns"

这是我在查资料的时候,从大佬那里抄来的。

最后,是根据源码,上面的两段函数,有其他写法。

anno.annotate_custom_annotations()
#等价于
anno.set_custom_annotations()
anno.annotate()
anno.apply_and_annotate()
#等价于
anno.apply()
anno.annotate()

参考:
cGitHub - trevismd/statannotations-tutorials: In this tutorial, we go over the features of statannotations

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值