You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整split violin图中statannot标注的水平偏移量?

解决statannot标注水平偏移问题(Split Violin图场景)

statannot本身确实没有提供水平偏移的参数,但可以通过手动修改标注对象位置或者自定义标注绘制逻辑来实现需求,以下是两种可行方案:

方案一:修改statannot生成的标注对象

先正常执行statannot的标注流程,再遍历生成的标注元素(文本、线段),调整它们的x坐标实现水平偏移。

示例代码

import seaborn as sns
import matplotlib.pyplot as plt
from statannot import add_stat_annotation

# 1. 绘制Split Violin图
tips = sns.load_dataset("tips")
sns.set(style="whitegrid")
ax = sns.violinplot(x="day", y="total_bill", hue="sex", data=tips, split=True)

# 2. 添加statannot标注
box_pairs = [(("Thur", "Male"), ("Thur", "Female")),
             (("Fri", "Male"), ("Fri", "Female"))]
test_results = add_stat_annotation(
    ax, data=tips, x="day", y="total_bill", hue="sex", split=True,
    box_pairs=box_pairs, test='t-test_ind', text_format='star',
    loc='inside', verbose=2
)

# 3. 手动调整水平偏移
offset = 0.1  # 偏移量,可根据需求调整正负和数值
for ann in test_results:
    # 调整标注文本的x坐标
    if hasattr(ann, 'text'):
        current_x = ann.get_x()
        ann.set_x(current_x + offset)
    # 调整标注线段的x坐标
    if hasattr(ann, 'get_segments'):
        segments = ann.get_segments()
        new_segments = []
        for seg in segments:
            # 线段每个点都应用水平偏移
            new_seg = [(x + offset, y) for x, y in seg]
            new_segments.append(new_seg)
        ann.set_segments(new_segments)

plt.show()

注意点

  • 偏移量offset需要根据你的图的x轴刻度位置调整:categorical类型的x轴,每个类别对应基础x坐标为0、1、2...,Split Violin的左右子部分通常分布在x±0.2区间内,可根据实际布局微调数值。
  • 不同的loc参数(如inside/outside)生成的标注对象类型可能不同,可通过print(type(ann))查看对象类型,针对性修改属性。

方案二:绕过statannot,手动绘制标注

直接用scipy计算T检验的p值,再用matplotlib的原生API绘制标注文本和线段,完全自主控制位置。

示例代码

import seaborn as sns
import matplotlib.pyplot as plt
from scipy.stats import ttest_ind

# 1. 绘制Split Violin图
tips = sns.load_dataset("tips")
sns.set(style="whitegrid")
ax = sns.violinplot(x="day", y="total_bill", hue="sex", data=tips, split=True)

# 2. 手动计算T检验p值
def get_pvalue(group1, group2):
    _, p = ttest_ind(group1, group2)
    return p

def p_to_star(p):
    """将p值转换为常用的星号标注"""
    if p < 0.001:
        return '***'
    elif p < 0.01:
        return '**'
    elif p < 0.05:
        return '*'
    else:
        return 'ns'

# 以Thur组为例
thur_male = tips[(tips['day'] == 'Thur') & (tips['sex'] == 'Male')]['total_bill']
thur_female = tips[(tips['day'] == 'Thur') & (tips['sex'] == 'Female')]['total_bill']
p_thur = get_pvalue(thur_male, thur_female)
star_thur = p_to_star(p_thur)

# 3. 自定义位置绘制标注
x_center = 0 + 0.1  # Thur对应的基础x是0,加0.1实现水平偏移
y_pos = thur_male.max() + 1  # 垂直位置设为组内最大值上方

# 绘制标注文本
ax.text(x_center, y_pos, star_thur, ha='center', va='bottom', fontweight='bold')
# 绘制连接两组的线段
ax.plot([0 - 0.1, 0 + 0.3], [y_pos - 0.5, y_pos - 0.5], color='black')

plt.show()

优势

  • 完全掌控标注的位置、样式,不受statannot的参数限制,适合对布局有精细要求的场景。

内容的提问来源于stack exchange,提问作者gaya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 16:05:26