python添加两组实验显著性

Python-statannotations库添加显著性标注:
1、官网资料:https://github.com/trevismd/statannotations
官网例子:

import seaborn as sns

from statannotations.Annotator import Annotator

df = sns.load_dataset("tips")
x = "day"
y = "total_bill"
order = ['Sun', 'Thur', 'Fri', 'Sat']

ax = sns.boxplot(data=df, x=x, y=y, order=order)

pairs=[("Thur", "Fri"), ("Thur", "Sat"), ("Fri", "Sun")]

annotator = Annotator(ax, pairs, data=df, x=x, y=y, order=order)
annotator.configure(test='Mann-Whitney', text_format='star', loc='outside')
annotator.apply_and_annotate()

结果:
在这里插入图片描述
分类箱线图添加显著性:

df = sns.load_dataset('diamonds')
df = df[df['color'].map(lambda x: x in 'EIJ')]
# Modifying data to yield unequal boxes in the hue value
df.loc[df['cut'] == 'Ideal', 'price'] = df.loc[df['cut'] == 'Ideal', 'price'].map(lambda x: min(x, 5000))
df.loc[df['cut'] == 'Premium', 'price'] = df.loc[df['cut'] == 'Premium', 'price'].map(lambda x: min(x, 7500))
df.loc[df['cut'] == 'Good', 'price'] = df.loc[df['cut'] == 'Good', 'price'].map(lambda x: min(x, 15000))
df.loc[df['cut'] == 'Very Good', 'price'] = df.loc[df['cut'] == 'Very Good', 'price'].map(lambda x: min(x, 3000))

x = "color"
y = "price"
hue = "cut"
hue_order=['Ideal', 'Premium', 'Good', 'Very Good', 'Fair']
order = ["E", "I", "J"]
pairs=[
    (("E", "Ideal"), ("E", "Very Good")),
    (("E", "Ideal"), ("E", "Premium")),
    (("E", "Ideal"), ("E", "Good")),
    (("I", "Ideal"), ("I", "Premium")),
    (("I", "Ideal"), ("I", "Good")),
    (("J", "Ideal"), ("J", "Premium")),
    (("J", "Ideal"), ("J", "Good")),
    (("E", "Good"), ("I", "Ideal")),
    (("I", "Premium"), ("J", "Ideal")),
    ]
ax = sns.boxplot(data=df, x=x, y=y, order=order, hue=hue, hue_order=hue_order)
annot.new_plot(ax, pairs, data=df, x=x, y=y, order=order, hue=hue, hue_order=hue_order)
annot.configure(test='Mann-Whitney', verbose=2)
annot.apply_test()
annot.annotate()
plt.legend(loc='upper left', bbox_to_anchor=(1.03, 1))
plt.savefig('example_hue_layout.png', dpi=300, bbox_inches='tight')

结果:
在这里插入图片描述

更多例子参考:
https://github.com/trevismd/statannotations/blob/master/usage/example.ipynb

2、具体实现过程【关键代码】:

from statannotations.Annotator import Annotator
#绘制显著性+星号
pairs_width=[(1, 2), (2, 3)]
annotator_width = Annotator(ax3, pairs_width, data=df, x='class', y='y', order=[1, 2, 3])
annotator_width.configure(test='Mann-Whitney', text_format='star',line_height=0.00001,line_width=0.5,fontsize=6)
annotator_width.apply_and_annotate()

3、属性设置
在这里插入图片描述
官网参数默认设置:

_DEFAULT_VALUES = {
    "alpha": 0.05,
    "test_short_name": None,
    "annotations": None,
    "_comparisons_correction": None,
    "_loc": "inside",
    "verbose": 1,
    "_just_configured": True,
    "show_test_name": True,
    "use_fixed_offset": False,
    "line_height": 0.02,
    "_test": None,
    "text_offset": 1,
    "color": '0.2',
    "line_width": 1.5,
    "custom_annotations": None,
    "hide_non_significant": False,#可以设置是否显示非显著性的标记
}

使用方法:

annotator_width.configure(test='Mann-Whitney', text_format='star',line_height=0.00001,line_width=0.5,fontsize=6)
annotator_width.apply_test().annotate(line_offset_to_group=0.2, line_offset=0.1)

4、检测方法

  • Mann-Whitney
  • t-test (independent and paired)
  • Welch’s t-test
  • Levene test
  • Wilcoxon test
  • Kruskal-Wallis test
  • Brunner-Munzel test
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值