如何将统计注释(例如 p 值)插入 seaborn 图形级图(例如 catplot)?

How does one insert statistical annotations (e.g. p-values) into a seaborn figure-level plot (e.g. catplot)?

目标:给定一个包含多行、分组条和映射条形图的 seaborn catplot (kind="bar"),我如何添加统计注释(p 值).

来自 的以下代码生成了我的图形,没有统计注释。我想在此图中插入统计注释:

import seaborn as sns

tips = sns.load_dataset("tips")

g = sns.catplot(x="sex", y="total_bill", hue="smoker", row="time", data=tips, kind="bar", ci = "sd", 
    edgecolor="black", errcolor="black", errwidth=1.5, capsize = 0.1, height=4, aspect=.7,alpha=0.5)

g.map(sns.stripplot, 'sex', 'total_bill', 'smoker', hue_order=['Yes', 'No'], order=['Male', 'Female'],
  palette=sns.color_palette(), dodge=True, alpha=0.6, ec='k', linewidth=1)


我试过的:我试过使用statannotations.Annotator.Annotator.plot_and_annotate_facets()。但是,我无法让它正常工作。

我也试过用statannotations.Annotator.Annotator.new_plot()。然而,这只适用于条形图而不适用于猫图。这是根据:

对应的代码
import seaborn as sns
from statannotations.Annotator import Annotator
%matplotlib inline
import matplotlib.pyplot as plt

df = sns.load_dataset("tips")

x="sex"
y="total_bill"
hue="smoker"
hue_order=['Yes', 'No']

pairs = [
    (("Male", "Yes"), ("Male", "No")),
    (("Female", "Yes"), ("Female", "No"))]

ax = sns.barplot(data=df, x=x, y=y, hue=hue, hue_order=hue_order, seed=2021, ci="sd", 
    edgecolor="black", errcolor="black", errwidth=1.5, capsize = 0.1, alpha=0.5)

sns.stripplot(x=x, y=y, hue=hue, data=df, dodge=True, alpha=0.6, ax=ax)

annot = Annotator(None, pairs)

annot.new_plot(ax, pairs, plot='barplot',
           data=df, x=x, y=y, hue=hue, hue_order=hue_order, seed=2021)
annot.configure(test='Mann-Whitney', text_format='simple', loc='inside', verbose=2)
annot.apply_test().annotate()

plt.legend(loc='upper left', bbox_to_anchor=(1.03, 1), title=hue)

问题:有谁知道如何将统计注释插入图形级别的绘图,最好是 catplot (kind="bar")?

我认为您可以遍历 FacetGrid 中的轴并明智地应用 Annotator 元素。

这是一个使用您提供的代码的简短示例:

import seaborn as sns
from statannotations.Annotator import Annotator
%matplotlib inline


tips = sns.load_dataset("tips")

args = dict(x="sex", y="total_bill", data=tips, hue="smoker", hue_order=["Yes","No"], order=['Male', 'Female'])

g = sns.catplot(edgecolor="black", errcolor="black", errwidth=1.5, capsize = 0.1, height=4, aspect=.7,alpha=0.5, kind="bar", ci = "sd", row="time", **args)
g.map(sns.stripplot, args["x"], args["y"], args["hue"], hue_order=args["hue_order"], order=args["order"], palette=sns.color_palette(), dodge=True, alpha=0.6, ec='k', linewidth=1)

pairs = [
    (("Male", "Yes"), ("Male", "No")),
    (("Female", "Yes"), ("Female", "No"))
]

for ax_n in g.axes:
    for ax in ax_n:
        annot = Annotator(ax, pairs, **args)
        annot.configure(test='Mann-Whitney', text_format='simple', loc='inside', verbose=2)
        annot.apply_test().annotate()

这会产生以下情节:

根据M. Sch.的回答,我们应该对原始数据进行子集table,否则,将对整个数据集进行统计:

import seaborn as sns
from statannotations.Annotator import Annotator


tips = sns.load_dataset("tips")

args = dict(x="sex", y="total_bill", hue="smoker", hue_order=["Yes","No"], order=['Male', 'Female'])

g = sns.catplot(edgecolor="black", errcolor="black", errwidth=1.5, capsize = 0.1, height=4, aspect=.7,alpha=0.5, kind="bar", ci = "sd", row="time", data=tips, **args)
g.map(sns.stripplot, args["x"], args["y"], args["hue"], hue_order=args["hue_order"], order=args["order"], palette=sns.color_palette(), dodge=True, alpha=0.6, ec='k', linewidth=1)

pairs = [
    (("Male", "Yes"), ("Male", "No")),
    (("Female", "Yes"), ("Female", "No"))
]

for name,ax in g.axes_dict.items():
# subset the table otherwise the stats were calculated on the whole dataset
        annot = Annotator(ax, pairs, **args,data=tips.loc[tips['time']==name,:]) 
        annot.configure(test='Mann-Whitney', text_format='simple', loc='inside', verbose=2)
        annot.apply_test().annotate()

输出图: output figure