如何将统计注释(星号或 p 值)插入 matplotlib / seaborn 图中?
How does one insert statistical annotations (stars or p-values) into matplotlib / seaborn plots?
这似乎是一个微不足道的问题,但我已经搜索了一段时间,似乎找不到答案。它似乎也应该成为这些软件包的标准部分。有谁知道在 seaborn 的分布图之间是否有标准的方法来包含统计注释?
例如,在两个框或群图之间?
如何向 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()
结果如下:
人们可能还对向不同的框对添加多个 注释感兴趣。在这种情况下,自动处理不同行和文本在 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))
这似乎是一个微不足道的问题,但我已经搜索了一段时间,似乎找不到答案。它似乎也应该成为这些软件包的标准部分。有谁知道在 seaborn 的分布图之间是否有标准的方法来包含统计注释?
例如,在两个框或群图之间?
如何向 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()
结果如下:
人们可能还对向不同的框对添加多个 注释感兴趣。在这种情况下,自动处理不同行和文本在 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))