【问题标题】:How does one insert statistical annotations (stars or p-values) into matplotlib / seaborn plots?如何将统计注释(星号或 p 值)插入 matplotlib / seaborn 图中?
【发布时间】:2016-08-03 08:55:44
【问题描述】:

这似乎是一个微不足道的问题,但我已经搜索了一段时间,似乎无法找到答案。它似乎也应该成为这些包的标准部分。有谁知道是否有一种标准方法可以在 seaborn 的分布图之间包含统计注释?

例如,在两个盒子或群图之间?

【问题讨论】:

  • 你需要拉出底层matplotlib Axes对象并使用Axes.text或Axes.annotate
  • 你碰巧有一个 R 例子可以比较吗? (MVCE!给我们任何带有代码的通用数据集,并解释你想要得到什么。)
  • 我所相信的一个很好的例子github.com/jbmouret/matplotlib_for_papers
  • 我认为@cancerconnector 需要的一个很好的例子可以在这里找到(在页面的最底部):github.com/jbmouret/matplotlib_for_papers 这个实现是纯 matplotlib,这里需要的是 p 值(星)注释应用于 seaborn 图。
  • 在 DTC 之后这么多年,我发现您在 SO!手动方法有效,但如果您尝试显示许多不同的比较,则会有点混乱。你找到其他方法了吗?谢谢。

标签: python-3.x matplotlib statistics seaborn


【解决方案1】:

这里如何向 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()

结果如下:

【讨论】:

  • 您将如何使用hue 实现这一点?
【解决方案2】:

人们也可能有兴趣将几个注释添加到不同的盒子对。在这种情况下,自动处理 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,
                    box_pairs=[("Thur", "Fri"), ("Thur", "Sat"), ("Fri", "Sun")],
                    test='Mann-Whitney', text_format='star', loc='outside', verbose=2)

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,
                    box_pairs=[(("Thur", "No"), ("Fri", "No")),
                                 (("Sat", "Yes"), ("Sat", "No")),
                                 (("Sun", "No"), ("Thur", "Yes"))
                                ],
                    test='t-test_ind', text_format='full', loc='inside', verbose=2)
plt.legend(loc='upper left', bbox_to_anchor=(1.03, 1))

【讨论】:

  • 函数名是“add_stat_annotation”,上面那个不起作用。您还需要定义 x 和 y: add_stat_annotation(ax, x="day", y="total_bill",df, [("Thur", "Fri"), ("Thur", "Sat"), (" Fri", "Sun")], test='t-test', order=None, textFormat='full', loc='inside', verbose=2)
  • 感谢您指出。我编辑了答案以反映 statannot 包中的更改。请注意,现在它也可以应用于具有色调类别的箱线图,如第二个示例所示。不幸的是,我们仍然需要为 add_stat_annotation 方法提供与用于生成 seaborn 箱线图的参数相同的 dataxyhue 参数。
  • 对此深表感谢!请问你为什么需要python3?它也可以在python2中使用吗?谢谢。
  • @NelsonGon 暂时没有。有关软件包功能的最新更新,请参阅 github 存储库。
  • @cjstevens,Statannot 没有得到积极维护。你可以看看 statannot 的一个分支,statannotations,它从 0.3.2 版开始优雅地支持条形图,使用与 statannot 完全相同的 API。最新 (alpha) 版本具有更多功能(和错误修复)和不同的用户界面。
猜你喜欢
  • 2019-10-15
  • 1970-01-01
  • 2016-03-19
  • 2012-08-27
  • 2018-08-07
  • 2019-12-09
  • 2022-11-02
  • 2020-09-04
  • 2019-09-25
相关资源
最近更新 更多