You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Statannotations v0.5中自定义p值阈值与alpha=0.1?

自定义Statannotations的p值显著性阈值(alpha=0.1)

问题背景

我是一名生物化学家,研究结果对经济学家朋友而言精度过高,他们要求将p值显著性水平设为10%(即alpha=0.1),而非标准的5%(0.05)。使用Statannotations v0.5时,尝试在annot.configure()中设置alpha=0.1报错,提示需修改pvalue_thresholds。希望用以下自定义阈值实现对应标注:

econ_pvalues= [[1e-3, "****"],
                [1e-2, "***"],
                [0.05, "**"],
                [0.1, "*"],
                [1, "ns"]]

解决方案

直接在annot.configure()中传入自定义的pvalue_thresholds参数,并同步设置alpha=0.1即可,无需额外调用format_annotations方法。修改后的完整代码如下:

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

df = sns.load_dataset('diamonds')
df = df[df['color'].map(lambda c: c in 'EIJ')]

x = "clarity"
y = "carat"
hue = "color"
hue_order=["E", "I", "J"]
pairs = [(("SI2", "E"), ("SI1", "E"))]
pairs = pairs + [((clar, 'I'), (clar, 'J')) for clar in df['clarity'].unique()]

# 定义经济学家所需的p值阈值(从严格到宽松排序)
econ_pvalues = [[1e-3, "****"],
                [1e-2, "***"],
                [0.05, "**"],
                [0.1, "*"],
                [1, "ns"]]

grph = sns.catplot(kind='bar', data=df, x=x, y=y, hue=hue, hue_order=hue_order)
for ax in grph.axes.ravel():
    annot = Annotator(ax, pairs, data=df, x=x, y=y, hue=hue, hue_order=hue_order)
    annot.configure(test='Kruskal',
                    comparisons_correction=None,
                    correction_format='replace',
                    text_format='star', 
                    line_offset=15, line_height=0.05, text_offset=1,
                    # 传入自定义阈值和目标alpha值
                    pvalue_thresholds=econ_pvalues,
                    alpha=0.1)
    annot.apply_test().annotate()

plt.legend(loc='upper left', bbox_to_anchor=(1.03, 1), title=hue)
plt.savefig('example_barplot_hue.png', dpi=300, bbox_inches='tight')
plt.show()

关键说明

  • 自定义阈值列表必须按p值从小到大(严格到宽松)排序,确保Statannotations能正确匹配对应的显著性符号
  • alpha=0.1会同步作为统计检验的显著性水平,与自定义阈值逻辑一致
  • 无需使用format_annotations.pval_annotation_text,该方法仅用于事后修改已生成的标注文本,无法替代配置阶段的阈值设置

内容的提问来源于stack exchange,提问作者Kateus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 23:45:34