Skip to content

Commit 36ac2b5

Browse files
committed
Added some tests and updated summary bars to allow full ax span or start directly from the contrast object
1 parent 5b9430f commit 36ac2b5

17 files changed

Lines changed: 141 additions & 70 deletions

dabest/_effsize_objects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ def plot(
11971197
Pass a list of indices of the contrast objects to have summary bars displayed on the plot.
11981198
For example, [0,1] will show summary bars for the first two contrast objects.
11991199
summary_bars_kwargs: dict, default None
1200-
If None, the following keywords are passed: {"color": None, "alpha": 0.15}
1200+
If None, the following keywords are passed: {"span_ax": False, "color": None, "alpha": 0.15}
12011201
delta_text : boolean, default True
12021202
Whether or not to display the text deltas.
12031203
delta_text_kwargs : dict, default None

dabest/misc_tools.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ def get_kwargs(plot_kwargs, ytick_color):
350350

351351
# Summary bars kwargs.
352352
default_summary_bars_kwargs = {
353+
"span_ax": False,
353354
"color": None,
354355
"alpha": 0.15
355356
}
@@ -617,23 +618,23 @@ def initialize_fig(plot_kwargs, dabest_obj, show_delta2, show_mini_meta, is_pair
617618
if show_delta2 or show_mini_meta:
618619
all_groups_count += 2
619620

620-
if is_paired and show_pairs and proportional is False:
621-
frac = 0.8
622-
else:
623-
frac = 1
624-
if float_contrast:
625-
height_inches = 4
626-
each_group_width_inches = 2.5 * frac
627-
else:
628-
height_inches = 6
629-
each_group_width_inches = 1.5 * frac
630-
631-
width_inches = each_group_width_inches * all_groups_count
632-
fig_size = (width_inches, height_inches)
633-
634621
if horizontal:
635622
frac = 0.3 if is_paired or show_mini_meta else 0.5
636623
fig_size = (7, 1 + (frac * all_groups_count))
624+
else:
625+
if is_paired and show_pairs and proportional is False:
626+
frac = 0.8
627+
else:
628+
frac = 1
629+
if float_contrast:
630+
height_inches = 4
631+
each_group_width_inches = 2.5 * frac
632+
else:
633+
height_inches = 6
634+
each_group_width_inches = 1.5 * frac
635+
636+
width_inches = each_group_width_inches * all_groups_count
637+
fig_size = (width_inches, height_inches)
637638

638639
init_fig_kwargs = dict(figsize=fig_size, dpi=plot_kwargs["dpi"], tight_layout=True)
639640

@@ -701,6 +702,7 @@ def initialize_fig(plot_kwargs, dabest_obj, show_delta2, show_mini_meta, is_pair
701702
]
702703
)
703704

705+
# Set axes
704706
contrast_axes = axins
705707
rawdata_axes.contrast_axes = axins
706708
table_axes = None
@@ -734,9 +736,12 @@ def initialize_fig(plot_kwargs, dabest_obj, show_delta2, show_mini_meta, is_pair
734736
fontsize_title = plot_kwargs["fontsize_title"]
735737
if title is not None:
736738
fig.suptitle(title, fontsize=fontsize_title)
739+
740+
# Set axes
737741
rawdata_axes = axx[0]
738742
contrast_axes = axx[1]
739743
table_axes = axx[2] if horizontal and show_table else None
744+
740745
rawdata_axes.set_frame_on(False)
741746
contrast_axes.set_frame_on(False)
742747
if horizontal and show_table:
@@ -783,17 +788,11 @@ def initialize_fig(plot_kwargs, dabest_obj, show_delta2, show_mini_meta, is_pair
783788
contrast_axes.set_ylim(contrast_ylim)
784789

785790
# Set raw axes y-label.
786-
swarm_label = plot_kwargs["swarm_label"]
787-
if swarm_label is None and yvar is None:
788-
swarm_label = "value"
789-
elif swarm_label is None and yvar is not None:
790-
swarm_label = yvar
791-
792-
bar_label = plot_kwargs["bar_label"]
793-
if bar_label is None and effect_size_type != "cohens_h":
794-
bar_label = "proportion of success"
795-
elif bar_label is None and effect_size_type == "cohens_h":
796-
bar_label = "value"
791+
swarm_label, bar_label = plot_kwargs["swarm_label"], plot_kwargs["bar_label"]
792+
if swarm_label is None:
793+
swarm_label = yvar if yvar is not None else "value"
794+
if bar_label is None:
795+
bar_label = "proportion of success" if effect_size_type != "cohens_h" else "value"
797796

798797
fontsize_rawylabel = plot_kwargs["fontsize_rawylabel"]
799798
rawdata_label = bar_label if proportional else swarm_label

dabest/plot_tools.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ def sankeydiag(
875875
def summary_bars_plotter(summary_bars: list, results: object, ax_to_plot: object,
876876
float_contrast: bool,summary_bars_kwargs: dict, ci_type: str,
877877
ticks_to_plot: list, color_col: str, plot_palette_raw: dict,
878-
proportional: bool, is_paired: bool):
878+
proportional: bool, is_paired: bool, horizontal: bool):
879879
"""
880880
Add summary bars to the contrast plot. Currently only functional for Vertical plots.
881881
@@ -903,6 +903,8 @@ def summary_bars_plotter(summary_bars: list, results: object, ax_to_plot: object
903903
Whether the data is proportional.
904904
is_paired : bool
905905
Whether the data is paired.
906+
horizontal : bool
907+
Whether the plot is horizontal.
906908
"""
907909
# Begin checks
908910
if not isinstance(summary_bars, list):
@@ -916,6 +918,8 @@ def summary_bars_plotter(summary_bars: list, results: object, ax_to_plot: object
916918
# End checks
917919
else:
918920
summary_xmin, summary_xmax = ax_to_plot.get_xlim()
921+
summary_ymin, summary_ymax = ax_to_plot.get_ylim()
922+
919923
summary_bars_colors = (
920924
[summary_bars_kwargs.get('color')]*int(max(ticks_to_plot)+1)
921925
if summary_bars_kwargs.get('color') is not None
@@ -924,6 +928,8 @@ def summary_bars_plotter(summary_bars: list, results: object, ax_to_plot: object
924928
else list(plot_palette_raw.values())
925929
)
926930
summary_bars_kwargs.pop('color')
931+
span_ax = summary_bars_kwargs.pop("span_ax")
932+
927933
for summary_index in summary_bars:
928934
if ci_type == "bca":
929935
summary_ci_low = results.bca_low[summary_index]
@@ -932,12 +938,28 @@ def summary_bars_plotter(summary_bars: list, results: object, ax_to_plot: object
932938
summary_ci_low = results.pct_low[summary_index]
933939
summary_ci_high = results.pct_high[summary_index]
934940

935-
summary_color = summary_bars_colors[int(ticks_to_plot[summary_index])]
936-
937-
ax_to_plot.add_patch(mpatches.Rectangle((summary_xmin,summary_ci_low),summary_xmax+1,
938-
summary_ci_high-summary_ci_low, zorder=-2, color=summary_color, **summary_bars_kwargs))
941+
if span_ax == True:
942+
starting_location = summary_ymax if horizontal else summary_xmin
943+
else:
944+
starting_location = ticks_to_plot[summary_index]
939945

946+
summary_color = summary_bars_colors[int(ticks_to_plot[summary_index])]
940947

948+
if horizontal:
949+
ax_to_plot.add_patch(mpatches.Rectangle(
950+
(summary_ci_low, starting_location),
951+
summary_ci_high-summary_ci_low, summary_ymin+1,
952+
zorder=-2, color=summary_color,
953+
**summary_bars_kwargs)
954+
)
955+
else:
956+
ax_to_plot.add_patch(mpatches.Rectangle(
957+
(starting_location, summary_ci_low),
958+
summary_xmax+1, summary_ci_high-summary_ci_low,
959+
zorder=-2, color=summary_color,
960+
**summary_bars_kwargs)
961+
)
962+
941963
def contrast_bars_plotter(results: object, ax_to_plot: object, swarm_plot_ax: object,
942964
ticks_to_plot: list, contrast_bars_kwargs: dict, color_col: str,
943965
plot_palette_raw: dict, show_mini_meta: bool, mini_meta_delta: object,

dabest/plotter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
501501

502502
# Summary bars WIP
503503
summary_bars = plot_kwargs["summary_bars"]
504-
if summary_bars is not None and not horizontal:
504+
if summary_bars is not None:
505505
summary_bars_plotter(
506506
summary_bars = summary_bars,
507507
results = results,
@@ -513,7 +513,8 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
513513
color_col = color_col,
514514
plot_palette_raw = plot_palette_raw,
515515
proportional = proportional,
516-
is_paired = is_paired
516+
is_paired = is_paired,
517+
horizontal = horizontal,
517518
)
518519
# Delta text WIP
519520
delta_text = plot_kwargs["delta_text"]

nbs/API/effsize_objects.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,7 @@
13561356
" Pass a list of indices of the contrast objects to have summary bars displayed on the plot.\n",
13571357
" For example, [0,1] will show summary bars for the first two contrast objects.\n",
13581358
" summary_bars_kwargs: dict, default None\n",
1359-
" If None, the following keywords are passed: {\"color\": None, \"alpha\": 0.15}\n",
1359+
" If None, the following keywords are passed: {\"span_ax\": False, \"color\": None, \"alpha\": 0.15}\n",
13601360
" delta_text : boolean, default True\n",
13611361
" Whether or not to display the text deltas.\n",
13621362
" delta_text_kwargs : dict, default None\n",

nbs/API/misc_tools.ipynb

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@
403403
"\n",
404404
" # Summary bars kwargs.\n",
405405
" default_summary_bars_kwargs = {\n",
406+
" \"span_ax\": False,\n",
406407
" \"color\": None, \n",
407408
" \"alpha\": 0.15\n",
408409
" }\n",
@@ -670,23 +671,23 @@
670671
" if show_delta2 or show_mini_meta:\n",
671672
" all_groups_count += 2\n",
672673
" \n",
673-
" if is_paired and show_pairs and proportional is False:\n",
674-
" frac = 0.8\n",
675-
" else:\n",
676-
" frac = 1\n",
677-
" if float_contrast:\n",
678-
" height_inches = 4\n",
679-
" each_group_width_inches = 2.5 * frac\n",
680-
" else:\n",
681-
" height_inches = 6\n",
682-
" each_group_width_inches = 1.5 * frac\n",
683-
"\n",
684-
" width_inches = each_group_width_inches * all_groups_count\n",
685-
" fig_size = (width_inches, height_inches) \n",
686-
"\n",
687674
" if horizontal:\n",
688675
" frac = 0.3 if is_paired or show_mini_meta else 0.5\n",
689676
" fig_size = (7, 1 + (frac * all_groups_count))\n",
677+
" else:\n",
678+
" if is_paired and show_pairs and proportional is False:\n",
679+
" frac = 0.8\n",
680+
" else:\n",
681+
" frac = 1\n",
682+
" if float_contrast:\n",
683+
" height_inches = 4\n",
684+
" each_group_width_inches = 2.5 * frac\n",
685+
" else:\n",
686+
" height_inches = 6\n",
687+
" each_group_width_inches = 1.5 * frac\n",
688+
"\n",
689+
" width_inches = each_group_width_inches * all_groups_count\n",
690+
" fig_size = (width_inches, height_inches) \n",
690691
"\n",
691692
" init_fig_kwargs = dict(figsize=fig_size, dpi=plot_kwargs[\"dpi\"], tight_layout=True)\n",
692693
"\n",
@@ -754,6 +755,7 @@
754755
" ]\n",
755756
" )\n",
756757
"\n",
758+
" # Set axes\n",
757759
" contrast_axes = axins\n",
758760
" rawdata_axes.contrast_axes = axins\n",
759761
" table_axes = None\n",
@@ -787,9 +789,12 @@
787789
" fontsize_title = plot_kwargs[\"fontsize_title\"]\n",
788790
" if title is not None:\n",
789791
" fig.suptitle(title, fontsize=fontsize_title)\n",
792+
"\n",
793+
" # Set axes \n",
790794
" rawdata_axes = axx[0]\n",
791795
" contrast_axes = axx[1]\n",
792796
" table_axes = axx[2] if horizontal and show_table else None\n",
797+
"\n",
793798
" rawdata_axes.set_frame_on(False)\n",
794799
" contrast_axes.set_frame_on(False)\n",
795800
" if horizontal and show_table:\n",
@@ -836,17 +841,11 @@
836841
" contrast_axes.set_ylim(contrast_ylim)\n",
837842
"\n",
838843
" # Set raw axes y-label.\n",
839-
" swarm_label = plot_kwargs[\"swarm_label\"]\n",
840-
" if swarm_label is None and yvar is None:\n",
841-
" swarm_label = \"value\"\n",
842-
" elif swarm_label is None and yvar is not None:\n",
843-
" swarm_label = yvar\n",
844-
"\n",
845-
" bar_label = plot_kwargs[\"bar_label\"]\n",
846-
" if bar_label is None and effect_size_type != \"cohens_h\":\n",
847-
" bar_label = \"proportion of success\"\n",
848-
" elif bar_label is None and effect_size_type == \"cohens_h\":\n",
849-
" bar_label = \"value\"\n",
844+
" swarm_label, bar_label = plot_kwargs[\"swarm_label\"], plot_kwargs[\"bar_label\"]\n",
845+
" if swarm_label is None:\n",
846+
" swarm_label = yvar if yvar is not None else \"value\"\n",
847+
" if bar_label is None:\n",
848+
" bar_label = \"proportion of success\" if effect_size_type != \"cohens_h\" else \"value\"\n",
850849
"\n",
851850
" fontsize_rawylabel = plot_kwargs[\"fontsize_rawylabel\"]\n",
852851
" rawdata_label = bar_label if proportional else swarm_label\n",

nbs/API/plot_tools.ipynb

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@
925925
"def summary_bars_plotter(summary_bars: list, results: object, ax_to_plot: object,\n",
926926
" float_contrast: bool,summary_bars_kwargs: dict, ci_type: str,\n",
927927
" ticks_to_plot: list, color_col: str, plot_palette_raw: dict, \n",
928-
" proportional: bool, is_paired: bool):\n",
928+
" proportional: bool, is_paired: bool, horizontal: bool):\n",
929929
" \"\"\"\n",
930930
" Add summary bars to the contrast plot. Currently only functional for Vertical plots.\n",
931931
"\n",
@@ -953,6 +953,8 @@
953953
" Whether the data is proportional.\n",
954954
" is_paired : bool\n",
955955
" Whether the data is paired.\n",
956+
" horizontal : bool\n",
957+
" Whether the plot is horizontal.\n",
956958
" \"\"\"\n",
957959
"# Begin checks \n",
958960
" if not isinstance(summary_bars, list):\n",
@@ -966,6 +968,8 @@
966968
"# End checks\n",
967969
" else:\n",
968970
" summary_xmin, summary_xmax = ax_to_plot.get_xlim()\n",
971+
" summary_ymin, summary_ymax = ax_to_plot.get_ylim()\n",
972+
"\n",
969973
" summary_bars_colors = (\n",
970974
" [summary_bars_kwargs.get('color')]*int(max(ticks_to_plot)+1)\n",
971975
" if summary_bars_kwargs.get('color') is not None\n",
@@ -974,6 +978,8 @@
974978
" else list(plot_palette_raw.values())\n",
975979
" )\n",
976980
" summary_bars_kwargs.pop('color')\n",
981+
" span_ax = summary_bars_kwargs.pop(\"span_ax\")\n",
982+
"\n",
977983
" for summary_index in summary_bars:\n",
978984
" if ci_type == \"bca\":\n",
979985
" summary_ci_low = results.bca_low[summary_index]\n",
@@ -982,12 +988,28 @@
982988
" summary_ci_low = results.pct_low[summary_index]\n",
983989
" summary_ci_high = results.pct_high[summary_index]\n",
984990
"\n",
985-
" summary_color = summary_bars_colors[int(ticks_to_plot[summary_index])]\n",
986-
"\n",
987-
" ax_to_plot.add_patch(mpatches.Rectangle((summary_xmin,summary_ci_low),summary_xmax+1, \n",
988-
" summary_ci_high-summary_ci_low, zorder=-2, color=summary_color, **summary_bars_kwargs))\n",
991+
" if span_ax == True:\n",
992+
" starting_location = summary_ymax if horizontal else summary_xmin\n",
993+
" else:\n",
994+
" starting_location = ticks_to_plot[summary_index] \n",
989995
"\n",
996+
" summary_color = summary_bars_colors[int(ticks_to_plot[summary_index])]\n",
990997
"\n",
998+
" if horizontal:\n",
999+
" ax_to_plot.add_patch(mpatches.Rectangle(\n",
1000+
" (summary_ci_low, starting_location),\n",
1001+
" summary_ci_high-summary_ci_low, summary_ymin+1, \n",
1002+
" zorder=-2, color=summary_color, \n",
1003+
" **summary_bars_kwargs)\n",
1004+
" )\n",
1005+
" else:\n",
1006+
" ax_to_plot.add_patch(mpatches.Rectangle(\n",
1007+
" (starting_location, summary_ci_low),\n",
1008+
" summary_xmax+1, summary_ci_high-summary_ci_low, \n",
1009+
" zorder=-2, color=summary_color, \n",
1010+
" **summary_bars_kwargs)\n",
1011+
" )\n",
1012+
" \n",
9911013
"def contrast_bars_plotter(results: object, ax_to_plot: object, swarm_plot_ax: object,\n",
9921014
" ticks_to_plot: list, contrast_bars_kwargs: dict, color_col: str, \n",
9931015
" plot_palette_raw: dict, show_mini_meta: bool, mini_meta_delta: object, \n",

nbs/API/plotter.ipynb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@
558558
"\n",
559559
" # Summary bars WIP\n",
560560
" summary_bars = plot_kwargs[\"summary_bars\"]\n",
561-
" if summary_bars is not None and not horizontal:\n",
561+
" if summary_bars is not None:\n",
562562
" summary_bars_plotter(\n",
563563
" summary_bars = summary_bars, \n",
564564
" results = results, \n",
@@ -570,7 +570,8 @@
570570
" color_col = color_col,\n",
571571
" plot_palette_raw = plot_palette_raw, \n",
572572
" proportional = proportional, \n",
573-
" is_paired = is_paired\n",
573+
" is_paired = is_paired,\n",
574+
" horizontal = horizontal,\n",
574575
" )\n",
575576
" # Delta text WIP\n",
576577
" delta_text = plot_kwargs[\"delta_text\"]\n",
-93 Bytes
Loading
40.3 KB
Loading

0 commit comments

Comments
 (0)