Skip to content

Commit dc0e518

Browse files
committed
feat(bar): add customizable y_base and interval for significance annotations
- Introduce new optional parameters `y_base` and `interval` to control the starting height and vertical spacing of statistical significance lines - Allow users to override automatic positioning by providing custom values; if not provided, fallback to internal calculation - Update documentation with detailed descriptions for both parameters in multiple function signatures - Reorder parameter `y_lim` to maintain consistent argument ordering across functions
1 parent 0d020ba commit dc0e518

1 file changed

Lines changed: 44 additions & 16 deletions

File tree

src/plotfig/bar.py

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
]
2929

3030

31-
3231
def _is_valid_data(data):
3332
if isinstance(data, np.ndarray):
3433
return data.ndim == 2
@@ -225,6 +224,8 @@ def _statistics(
225224
statistical_line_color,
226225
asterisk_fontsize,
227226
asterisk_color,
227+
y_base,
228+
interval,
228229
):
229230
if isinstance(test_method, list):
230231
if len(test_method) > 2 or (
@@ -240,7 +241,8 @@ def _statistics(
240241
return
241242

242243
y_max = ax.get_ylim()[1]
243-
interval = (y_max - np.max(all_values)) / (len(comparisons) + 1)
244+
y_base = y_base or np.max(all_values)
245+
interval = interval or (y_max - np.max(all_values)) / (len(comparisons) + 1)
244246

245247
color = (
246248
"b"
@@ -251,7 +253,7 @@ def _statistics(
251253
_annotate_significance(
252254
ax,
253255
comparisons,
254-
np.max(all_values),
256+
y_base,
255257
interval,
256258
line_color=statistical_line_color,
257259
star_offset=interval / 5,
@@ -269,11 +271,13 @@ def _statistics(
269271
return
270272

271273
y_max = ax.get_ylim()[1]
272-
interval = (y_max - np.max(all_values)) / (len(comparisons) + 1)
274+
y_base = y_base or np.max(all_values)
275+
interval = interval or (y_max - np.max(all_values)) / (len(comparisons) + 1)
276+
273277
_annotate_significance(
274278
ax,
275279
comparisons,
276-
np.max(all_values),
280+
y_base,
277281
interval,
278282
line_color=statistical_line_color,
279283
star_offset=interval / 5,
@@ -293,7 +297,6 @@ def plot_one_group_bar_figure(
293297
colors_end: list[str] | None = None,
294298
show_dots: bool = True,
295299
dots_color: list[list[str]] | None = None,
296-
y_lim: list[Num] | tuple[Num, Num] | None = None,
297300
width: Num = 0.5,
298301
color_alpha: Num = 1,
299302
dots_size: Num = 35,
@@ -310,13 +313,16 @@ def plot_one_group_bar_figure(
310313
y_label_fontsize: Num = 10,
311314
y_tick_fontsize: Num = 8,
312315
y_tick_rotation: Num = 0,
316+
y_lim: list[Num] | tuple[Num, Num] | None = None,
313317
statistic: bool = False,
314318
test_method: list[str] = ["ttest_ind"],
315319
p_list: list[float] | None = None,
316320
popmean: Num = 0,
317321
statistical_line_color: str = "0.5",
318322
asterisk_fontsize: Num = 10,
319323
asterisk_color: str = "k",
324+
y_base: float | None = None,
325+
interval: float | None = None,
320326
ax_bottom_is_0: bool = False,
321327
y_max_tick_is_1: bool = False,
322328
math_text: bool = True,
@@ -346,8 +352,6 @@ def plot_one_group_bar_figure(
346352
是否显示散点. Defaults to True.
347353
dots_color (list[list[str]] | None, optional):
348354
散点的颜色列表. Defaults to None.
349-
y_lim (list[Num] | tuple[Num, Num] | None, optional):
350-
Y轴的范围限制. Defaults to None.
351355
width (Num, optional):
352356
柱状图的宽度. Defaults to 0.5.
353357
color_alpha (Num, optional):
@@ -380,6 +384,8 @@ def plot_one_group_bar_figure(
380384
Y轴刻度字体大小. Defaults to 8.
381385
y_tick_rotation (Num, optional):
382386
Y轴刻度旋转角度. Defaults to 0.
387+
y_lim (list[Num] | tuple[Num, Num] | None, optional):
388+
Y轴的范围限制. Defaults to None.
383389
statistic (bool, optional):
384390
是否进行统计显著性分析. Defaults to False.
385391
test_method (list[str], optional):
@@ -400,6 +406,10 @@ def plot_one_group_bar_figure(
400406
显著性星号的字体大小. Defaults to 10.
401407
asterisk_color (str, optional):
402408
显著性星号的颜色. Defaults to "k".
409+
y_base (float | None, optional):
410+
显著性连线的起始Y轴位置(高度)。如果为None,则使用内部算法自动计算一个合适的位置。Defaults to None.
411+
interval (float | None, optional):
412+
相邻显著性连线之间的垂直距离(Y轴增量)。如果为None,则使用内部算法根据图表范围和比较对数自动计算。Defaults to None.
403413
ax_bottom_is_0 (bool, optional):
404414
Y轴是否从0开始. Defaults to False.
405415
y_max_tick_is_1 (bool, optional):
@@ -549,6 +559,8 @@ def plot_one_group_bar_figure(
549559
statistical_line_color,
550560
asterisk_fontsize,
551561
asterisk_color,
562+
y_base,
563+
interval,
552564
)
553565
return ax
554566

@@ -577,14 +589,16 @@ def plot_one_group_violin_figure(
577589
y_label_fontsize: Num = 10,
578590
y_tick_fontsize: Num = 8,
579591
y_tick_rotation: Num = 0,
592+
y_lim: list[Num] | tuple[Num, Num] | None = None,
580593
statistic: bool = False,
581594
test_method: list[str] = ["ttest_ind"],
582595
popmean: Num = 0,
583596
p_list: list[float] | None = None,
584597
statistical_line_color: str = "0.5",
585598
asterisk_fontsize: Num = 10,
586599
asterisk_color: str = "k",
587-
y_lim: list[Num] | tuple[Num, Num] | None = None,
600+
y_base: float | None = None,
601+
interval: float | None = None,
588602
ax_bottom_is_0: bool = False,
589603
y_max_tick_is_1: bool = False,
590604
math_text: bool = True,
@@ -640,6 +654,8 @@ def plot_one_group_violin_figure(
640654
Y轴刻度字体大小. Defaults to 8.
641655
y_tick_rotation (Num, optional):
642656
Y轴刻度旋转角度. Defaults to 0.
657+
y_lim (list[Num] | tuple[Num, Num] | None, optional):
658+
Y轴的范围限制. Defaults to None.
643659
statistic (bool, optional):
644660
是否进行统计显著性分析. Defaults to False.
645661
test_method (list[str], optional):
@@ -654,8 +670,10 @@ def plot_one_group_violin_figure(
654670
显著性星号的字体大小. Defaults to 10.
655671
asterisk_color (str, optional):
656672
显著性星号的颜色. Defaults to "k".
657-
y_lim (list[Num] | tuple[Num, Num] | None, optional):
658-
Y轴的范围限制. Defaults to None.
673+
y_base (float | None, optional):
674+
显著性连线的起始Y轴位置(高度)。如果为None,则使用内部算法自动计算一个合适的位置。Defaults to None.
675+
interval (float | None, optional):
676+
相邻显著性连线之间的垂直距离(Y轴增量)。如果为None,则使用内部算法根据图表范围和比较对数自动计算。Defaults to None.
659677
ax_bottom_is_0 (bool, optional):
660678
Y轴是否从0开始. Defaults to False.
661679
y_max_tick_is_1 (bool, optional):
@@ -830,6 +848,8 @@ def _draw_gradient_violin(ax, data, pos, width, c1, c2, color_alpha):
830848
statistical_line_color,
831849
asterisk_fontsize,
832850
asterisk_color,
851+
y_base,
852+
interval,
833853
)
834854

835855
return ax
@@ -860,13 +880,15 @@ def plot_multi_group_bar_figure(
860880
y_label_fontsize=10,
861881
y_tick_fontsize=8,
862882
y_tick_rotation=0,
883+
y_lim: list[Num] | tuple[Num, Num] | None = None,
863884
statistic: bool = False,
864885
test_method: str = "external",
865886
p_list: list[list[Num]] | None = None,
866887
line_color="0.5",
867888
asterisk_fontsize=10,
868889
asterisk_color="k",
869-
y_lim: list[Num] | tuple[Num, Num] | None = None,
890+
y_base: float | None = None,
891+
interval: float | None = None,
870892
ax_bottom_is_0: bool = False,
871893
y_max_tick_is_1: bool = False,
872894
math_text: bool = True,
@@ -924,6 +946,8 @@ def plot_multi_group_bar_figure(
924946
Y轴刻度字体大小. Defaults to 8.
925947
y_tick_rotation (int, optional):
926948
Y轴刻度旋转角度. Defaults to 0.
949+
y_lim (list[Num] | tuple[Num, Num] | None, optional):
950+
Y轴的范围限制. Defaults to None.
927951
statistic (bool, optional):
928952
是否进行统计显著性分析. Defaults to False.
929953
test_method (str, optional):
@@ -936,8 +960,10 @@ def plot_multi_group_bar_figure(
936960
显著性星号的字体大小. Defaults to 10.
937961
asterisk_color (str, optional):
938962
显著性星号的颜色. Defaults to "k".
939-
y_lim (list[Num] | tuple[Num, Num] | None, optional):
940-
Y轴的范围限制. Defaults to None.
963+
y_base (float | None, optional):
964+
显著性连线的起始Y轴位置(高度)。如果为None,则使用内部算法自动计算一个合适的位置。Defaults to None.
965+
interval (float | None, optional):
966+
相邻显著性连线之间的垂直距离(Y轴增量)。如果为None,则使用内部算法根据图表范围和比较对数自动计算。Defaults to None.
941967
ax_bottom_is_0 (bool, optional):
942968
Y轴是否从0开始. Defaults to False.
943969
y_max_tick_is_1 (bool, optional):
@@ -1067,11 +1093,13 @@ def plot_multi_group_bar_figure(
10671093
if p <= 0.05:
10681094
comparisons.append((x_positions[i], x_positions[j], p))
10691095
y_max = ax.get_ylim()[1]
1070-
interval = (y_max - np.max(all_values)) / (len(comparisons) + 1)
1096+
y_base = y_base or np.max(all_values)
1097+
interval = interval or (y_max - np.max(all_values)) / (len(comparisons) + 1)
1098+
10711099
_annotate_significance(
10721100
ax,
10731101
comparisons,
1074-
np.max(all_values),
1102+
y_base,
10751103
interval,
10761104
line_color=line_color,
10771105
star_offset=interval / 5,

0 commit comments

Comments
 (0)