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