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
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
