import seaborn as sns
import pandas as pd
from statannotations.Annotator import Annotator
import matplotlib.pyplot as plt
# 加载示例数据集
tips = sns.load_dataset('tips')
# 使用 catplot 绘制水平子图
g = sns.catplot(
x="sex",
y="total_bill",
hue="smoker",
col="day", # 按 time 变量拆分水平子图
data=tips,
kind="bar", # 使用柱状图
height=4,
aspect=0.7,
ci="sd", # 显示标准差误差条
edgecolor="black",
errcolor="black",
errwidth=1.5,
capsize=0.1,
alpha=0.5,
# sharey=False,
)
# 添加 stripplot 以显示数据点
g.map(sns.stripplot, 'sex', 'total_bill', 'smoker',
hue_order=['Yes', 'No'],
order=['Male', 'Female'],
palette=sns.color_palette(),
dodge=True,
alpha=0.6,
linewidth=1)
# 定义需要比较的组对
pairs = [
(("Male", "Yes"), ("Male", "No"),),
(("Male", "Yes"), ("Female", "Yes"),),
(("Male", "Yes"), ("Female", "No"),),
]
# 为每个子图添加显著性标记
for ax in g.axes.flat:
annotator = Annotator(
ax,
pairs,
data=tips,
x="sex",
y="total_bill",
hue="smoker",
# hue_order=['Yes', 'No'],
# order=['Male', 'Female']
)
annotator.configure(
test='Mann-Whitney', # 选择统计检验方法
text_format='star', # 显示显著性星号(*表示 p<0.05, **表示 p<0.01 等)
loc='inside', # 标记位置
verbose=0 # 显示详细信息
)
annotator.apply_test().annotate()
# 显示图形
plt.show()
@trevismd Thank you for your excellent work on this tool; it is incredibly convenient and truly indispensable in the Python ecosystem.
I have encountered an issue during my use. The spacing between elements appears to accumulate progressively across subplots, leading to a loss of positional uniformity. I have attempted to resolve this by examining the source code and trying methods such as
annotator.new_plotandannotator.reset_configuration(), but without success.If you have time, could you please point me toward the part of the codebase that might be responsible for this cumulative spacing effect? I am more than willing to explore and modify the source code myself to achieve a usable plotting result as soon as possible.
Thank you again for developing such a valuable tool.
When
sharey=Trueis set, the vertical spacing between the lines progressively increases across the subplots.sharey=True codes
When
sharey=Falseis set, the spacing between the lines appears uniform across the subplots.sharey=False codes
However, when I apply a similar code to my own dataset, the positions of the asterisk markers become progressively misaligned across the multiple subplots.
my own datasets