-
Notifications
You must be signed in to change notification settings - Fork 79
Description
@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_plot and annotator.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=True is set, the vertical spacing between the lines progressively increases across the subplots.
sharey=True codes
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()
When sharey=False is set, the spacing between the lines appears uniform across the subplots.
sharey=False codes
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()
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
import seaborn as sns
import pandas as pd
from statannotations.Annotator import Annotator
import plotnine as pw
plotting_parameters = {
"x": "",
"y": "",
'orient': 'h',
}
# 创建 catplot
g = sns.catplot(
data=all_df_sorted,
**plotting_parameters,
hue="seqType",
fill=False,
kind="violin",
col="Method",
cut=0,
# height=6,
sharex=False,
)
# annotator = Annotator.get_empty_annotator()
# 遍历每个分面(ax)
for ax in g.axes.flat:
# # 获取当前子图对应的 Method
# method_name = ax.get_title().split(" = ")[1]
# # # 初始化 Annotator
# annotator.new_plot(
# ax,
# pairs=pairs,
# data=all_df_sorted[all_df_sorted["Method"] == method_name],
# plot='violinplot',
# **plotting_parameters,
# )
annotator = Annotator(
ax,
pairs=pairs,
# data=all_df_sorted[all_df_sorted["Method"] == method_name],
data=all_df_sorted,
**plotting_parameters,
)
# annotator.reset_configuration()
# 配置统计检验,统一显著性标记线的间距
annotator.configure(
comparisons_correction="Bonferroni",
test="t-test_ind",
# test="Mann-Whitney",
# text_format="star",
loc="inside",
verbose=False, # 关闭调试输出
# line_height=0, # 多组显著性线之间的间距
# text_offset=0,
)
# 执行检验并绘制注释
annotator.apply_and_annotate()