matplotlib 如何插入统计注解(例如p值)转换成海运图形水平图(例如catplot)?

u5i3ibmn  于 2023-05-18  发布在  其他
关注(0)|答案(3)|浏览(90)

Goal:给定一个带有多行、分组条形图和Map条形图的seaborn catplot(kind=“bar”),如何添加统计注解(p值)。

下面的代码来自@特伦顿McKinney生成了我的数字,没有统计注解。我想在这个图中插入统计注解:

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()*。然而,这只适用于barplot,而不适用于catplot。这是基于@r-beginners的相应代码:

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”)?

ou6hu8tu

ou6hu8tu1#

继M. Sch.的答案,我们应该对原始数据表进行子集化,否则,统计数据将在整个数据集上计算:

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

kcrjzv8t

kcrjzv8t2#

我认为你可以迭代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()

这将生成以下图:

mwecs4sa

mwecs4sa3#

我设法让Annotator.plot_and_annotate_facets()工作。它没有很好的文档记录,并且有点不自然,但是为了完整起见,请在下面找到它:

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

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)

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

ant = Annotator(None, pairs)
kwargs = {
    'plot_params': { # this takes what normally goes into sns.barplot etc.
        'x': 'sex',
        'y': 'total_bill',
        'hue': 'smoker',
        'hue_order': ['Yes', 'No']
    },
    'annotation_func': 'apply_test', # has three options
    'configuration': {'test': 'wilcox'}, # this takes what normally goes into ant.configure
    'plot': 'boxplot'
}

g.map_dataframe(ant.plot_and_annotate_facets, **kwargs)
plt.show()

结果图如下所示:

相关问题