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

如何自定义statannotations中星号格式的p值阈值?

statannotations自定义p值星号阈值设置方法

要实现自定义p值与星号的对应规则,只需在annot.configure()方法中,通过**pvalue_thresholds**参数传入格式为{p值阈值: 星号标记}的字典即可。

注意事项

  • 阈值需按从大到小的顺序定义,工具会匹配第一个满足p ≤ 阈值的规则;
  • 建议保留1.0: 'ns'的映射,避免无显著性的p值出现无匹配标记的情况。

符合需求的自定义阈值字典

根据你需要的规则(*对应p≤0.0001,**对应p≤0.00001,***对应p≤0.000001),构造的字典如下:

custom_thresholds = {
    1.0: 'ns',
    0.0001: '*',
    0.00001: '**',
    0.000001: '***'
}

修改后的完整示例代码

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

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)
annot = Annotator(ax, [("Thur", "Fri"), ("Thur", "Sat"), ("Fri", "Sun")], data=df, x=x, y=y, order=order)

# 自定义p值阈值映射
custom_thresholds = {
    1.0: 'ns',
    0.0001: '*',
    0.00001: '**',
    0.000001: '***'
}

annot.configure(
    test='Mann-Whitney',
    text_format='star',
    loc='outside',
    verbose=2,
    pvalue_thresholds=custom_thresholds  # 传入自定义阈值参数
)
annot.apply_test()
ax, test_results = annot.annotate()
plt.savefig('example_custom_thresholds.png', dpi=300, bbox_inches='tight')

运行代码后,设置verbose=2会在控制台输出自定义的阈值规则,可验证是否符合预期。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 08:36:09