can*_*tor 26 statistics matplotlib python-3.x seaborn
这似乎是一个微不足道的问题,但我一直在寻找一段时间,似乎无法找到答案.它似乎也应该是这些软件包的标准部分.有没有人知道是否有标准的方法在seaborn中的分布图之间包含统计注释?
例如,在两个盒子或者swarmplots之间?
Ulr*_*ern 32
这里是如何在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()
Run Code Online (Sandbox Code Playgroud)
小智 18
人们也可能有兴趣将几个注释添加到不同的盒子对.在这种情况下,自动处理y轴上不同线条和文本的放置可能是有用的.我写了一个小函数来处理这些情况(参见Github repo),它正确地将行一个堆叠在一起而不重叠.注释可以在图的内部或外部.这是一个最小的例子.
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)
Run Code Online (Sandbox Code Playgroud)
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))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11302 次 |
| 最近记录: |