如何将统计注释(星号或p值)插入matplotlib / seaborn图?

时间:2016-04-12 15:59:17

标签: python-3.x matplotlib statistics seaborn

这似乎是一个微不足道的问题,但我一直在寻找,似乎无法找到答案。它似乎也应该是这些软件包的标准部分。有没有人知道是否有标准方法在seaborn中的分布图之间包含统计注释?

例如,在两个盒子或者swarmplots之间?

Example: the yellow distribution is significantly different than the others (by wilcoxon - how can i display that visually?

2 个答案:

答案 0 :(得分:29)

这里是如何将统计注释添加到Seaborn盒子图:

import seaborn as sns, matplotlib.pyplot as plt

tips = sns.load_dataset("tips")
sns.boxplot(x="day", y="total_bill", data=tips, palette="PRGn")

# statistical annotation
x1, x2 = 2, 3   # columns 'Sat' and 'Sun' (first column: 0, see plt.xticks())
y, h, col = tips['total_bill'].max() + 2, 2, 'k'
plt.plot([x1, x1, x2, x2], [y, y+h, y+h, y], lw=1.5, c=col)
plt.text((x1+x2)*.5, y+h, "ns", ha='center', va='bottom', color=col)

plt.show()

结果如下: box plot annotated

答案 1 :(得分:12)

可能还有兴趣将几个注释添加到不同的盒子对中。在这种情况下,自动处理y轴上不同行和文本的位置可能很有用。我和其他贡献者编写了一个处理这些情况的小函数(请参见Github repo),该函数正确地将行彼此叠放而不重叠。注释可以在图的内部或外部,并且可以执行多种统计检验:Mann-Whitney和t检验(独立和成对)。这是一个最小的例子。

import matplotlib.pyplot as plt
import seaborn as sns
from statannot import add_stat_annotation

sns.set(style="whitegrid")
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)
add_stat_annotation(ax, data=df, x=x, y=y, order=order,
                    boxPairList=[("Thur", "Fri"), ("Thur", "Sat"), ("Fri", "Sun")],
                    test='Mann-Whitney', textFormat='star', loc='outside', verbose=2)

example1

x = "day"
y = "total_bill"
hue = "smoker"
ax = sns.boxplot(data=df, x=x, y=y, hue=hue)
add_stat_annotation(ax, data=df, x=x, y=y, hue=hue,
                    boxPairList=[(("Thur", "No"), ("Fri", "No")),
                                 (("Sat", "Yes"), ("Sat", "No")),
                                 (("Sun", "No"), ("Thur", "Yes"))
                                ],
                    test='t-test_ind', textFormat='full', loc='inside', verbose=2)
plt.legend(loc='upper left', bbox_to_anchor=(1.03, 1))

example2