Skip to content

Commit d91f285

Browse files
committed
Further trimming to plotter and created new function in misc_tools for general aesthetic changes, and then also altered the fig size for show pairs+paired plots+float_contrast
1 parent 36e3533 commit d91f285

5 files changed

Lines changed: 309 additions & 265 deletions

File tree

dabest/_modidx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
'dabest/misc_tools.py'),
7070
'dabest.misc_tools.Gardner_Altman_Plot_Aesthetic_Adjustments': ( 'API/misc_tools.html#gardner_altman_plot_aesthetic_adjustments',
7171
'dabest/misc_tools.py'),
72+
'dabest.misc_tools.General_Plot_Aesthetic_Adjustments': ( 'API/misc_tools.html#general_plot_aesthetic_adjustments',
73+
'dabest/misc_tools.py'),
7274
'dabest.misc_tools.add_counts_to_ticks': ( 'API/misc_tools.html#add_counts_to_ticks',
7375
'dabest/misc_tools.py'),
7476
'dabest.misc_tools.extract_contrast_plotting_ticks': ( 'API/misc_tools.html#extract_contrast_plotting_ticks',

dabest/misc_tools.py

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
__all__ = ['merge_two_dicts', 'unpack_and_add', 'print_greeting', 'get_varname', 'get_params', 'get_kwargs', 'get_color_palette',
55
'initialize_fig', 'get_plot_groups', 'add_counts_to_ticks', 'extract_contrast_plotting_ticks',
66
'set_xaxis_ticks_and_lims', 'show_legend', 'Gardner_Altman_Plot_Aesthetic_Adjustments',
7-
'Cumming_Plot_Aesthetic_Adjustments']
7+
'Cumming_Plot_Aesthetic_Adjustments', 'General_Plot_Aesthetic_Adjustments']
88

99
# %% ../nbs/API/misc_tools.ipynb 4
1010
import datetime as dt
@@ -389,7 +389,7 @@ def initialize_fig(plot_kwargs, dabest_obj, show_delta2, show_mini_meta, is_pair
389389
if show_delta2 or show_mini_meta:
390390
all_groups_count += 2
391391
if is_paired and show_pairs and proportional is False:
392-
frac = 0.75
392+
frac = 0.8
393393
else:
394394
frac = 1
395395
if float_contrast:
@@ -617,7 +617,7 @@ def show_legend(legend_labels, legend_handles, rawdata_axes, contrast_axes, floa
617617
if float_contrast:
618618
axes_with_legend = contrast_axes
619619
if show_pairs:
620-
bta = (1.75, 1.02)
620+
bta = (2.00, 1.02)
621621
else:
622622
bta = (1.5, 1.02)
623623
else:
@@ -934,4 +934,110 @@ def Cumming_Plot_Aesthetic_Adjustments(plot_kwargs, show_delta2, effect_size_typ
934934

935935
ax.set_ylim(ylim)
936936
del redraw_axes_kwargs["y"]
937-
...
937+
938+
def General_Plot_Aesthetic_Adjustments(show_delta2, show_mini_meta, contrast_axes, redraw_axes_kwargs, plot_kwargs,
939+
yvar, effect_size_type, proportional, effectsize_df, is_paired, float_contrast,
940+
rawdata_axes, og_ylim_raw, effect_size):
941+
942+
if show_delta2 or show_mini_meta:
943+
ylim = contrast_axes.get_ylim()
944+
redraw_axes_kwargs["y"] = ylim[0]
945+
x_ticks = contrast_axes.get_xticks()
946+
contrast_axes.hlines(xmin=x_ticks[-2], xmax=x_ticks[-1], **redraw_axes_kwargs)
947+
del redraw_axes_kwargs["y"]
948+
949+
# Set raw axes y-label.
950+
swarm_label = plot_kwargs["swarm_label"]
951+
if swarm_label is None and yvar is None:
952+
swarm_label = "value"
953+
elif swarm_label is None and yvar is not None:
954+
swarm_label = yvar
955+
956+
bar_label = plot_kwargs["bar_label"]
957+
if bar_label is None and effect_size_type != "cohens_h":
958+
bar_label = "proportion of success"
959+
elif bar_label is None and effect_size_type == "cohens_h":
960+
bar_label = "value"
961+
962+
# Place contrast axes y-label.
963+
contrast_label_dict = {
964+
"mean_diff": "mean difference",
965+
"median_diff": "median difference",
966+
"cohens_d": "Cohen's d",
967+
"hedges_g": "Hedges' g",
968+
"cliffs_delta": "Cliff's delta",
969+
"cohens_h": "Cohen's h",
970+
"delta_g": "mean difference",
971+
}
972+
973+
if proportional and effect_size_type != "cohens_h":
974+
default_contrast_label = "proportion difference"
975+
elif effect_size_type == "delta_g":
976+
default_contrast_label = "Hedges' g"
977+
else:
978+
default_contrast_label = contrast_label_dict[effectsize_df.effect_size]
979+
980+
if plot_kwargs["contrast_label"] is None:
981+
if is_paired:
982+
contrast_label = "paired\n{}".format(default_contrast_label)
983+
else:
984+
contrast_label = default_contrast_label
985+
contrast_label = contrast_label.capitalize()
986+
else:
987+
contrast_label = plot_kwargs["contrast_label"]
988+
989+
if plot_kwargs["fontsize_rawylabel"] is not None:
990+
fontsize_rawylabel = plot_kwargs["fontsize_rawylabel"]
991+
if plot_kwargs["fontsize_contrastylabel"] is not None:
992+
fontsize_contrastylabel = plot_kwargs["fontsize_contrastylabel"]
993+
if plot_kwargs["fontsize_delta2label"] is not None:
994+
fontsize_delta2label = plot_kwargs["fontsize_delta2label"]
995+
996+
contrast_axes.set_ylabel(contrast_label, fontsize=fontsize_contrastylabel)
997+
if float_contrast:
998+
contrast_axes.yaxis.set_label_position("right")
999+
1000+
# Set the rawdata axes labels appropriately
1001+
if not proportional:
1002+
rawdata_axes.set_ylabel(swarm_label, fontsize=fontsize_rawylabel)
1003+
else:
1004+
rawdata_axes.set_ylabel(bar_label, fontsize=fontsize_rawylabel)
1005+
rawdata_axes.set_xlabel("")
1006+
1007+
# Because we turned the axes frame off, we also need to draw back
1008+
# the y-spine for both axes.
1009+
if not float_contrast:
1010+
rawdata_axes.set_xlim(contrast_axes.get_xlim())
1011+
og_xlim_raw = rawdata_axes.get_xlim()
1012+
rawdata_axes.vlines(
1013+
og_xlim_raw[0], og_ylim_raw[0], og_ylim_raw[1], **redraw_axes_kwargs
1014+
)
1015+
1016+
og_xlim_contrast = contrast_axes.get_xlim()
1017+
1018+
if float_contrast:
1019+
xpos = og_xlim_contrast[1]
1020+
else:
1021+
xpos = og_xlim_contrast[0]
1022+
1023+
og_ylim_contrast = contrast_axes.get_ylim()
1024+
contrast_axes.vlines(
1025+
xpos, og_ylim_contrast[0], og_ylim_contrast[1], **redraw_axes_kwargs
1026+
)
1027+
1028+
if show_delta2:
1029+
if plot_kwargs["delta2_label"] is not None:
1030+
delta2_label = plot_kwargs["delta2_label"]
1031+
elif effect_size == "mean_diff":
1032+
delta2_label = "delta - delta"
1033+
else:
1034+
delta2_label = "deltas' g"
1035+
delta2_axes = contrast_axes.twinx()
1036+
delta2_axes.set_frame_on(False)
1037+
delta2_axes.set_ylabel(delta2_label, fontsize=fontsize_delta2label)
1038+
og_xlim_delta = contrast_axes.get_xlim()
1039+
og_ylim_delta = contrast_axes.get_ylim()
1040+
delta2_axes.set_ylim(og_ylim_delta)
1041+
delta2_axes.vlines(
1042+
og_xlim_delta[1], og_ylim_delta[0], og_ylim_delta[1], **redraw_axes_kwargs
1043+
)

dabest/plotter.py

Lines changed: 44 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
7070
show_legend,
7171
Gardner_Altman_Plot_Aesthetic_Adjustments,
7272
Cumming_Plot_Aesthetic_Adjustments,
73+
General_Plot_Aesthetic_Adjustments,
7374
)
7475
from .plot_tools import (
7576
get_swarm_spans,
@@ -138,7 +139,8 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
138139
plot_palette_bar, plot_palette_contrast, plot_palette_sankey) = get_color_palette(plot_kwargs=plot_kwargs,
139140
plot_data=plot_data,
140141
xvar=xvar,
141-
show_pairs=show_pairs)
142+
show_pairs=show_pairs
143+
)
142144

143145
# Initialise the figure.
144146
fig, rawdata_axes, contrast_axes, swarm_ylim = initialize_fig(plot_kwargs=plot_kwargs,
@@ -300,13 +302,6 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
300302
plot_kwargs=plot_kwargs
301303
)
302304

303-
# Save the handles and labels for the legend.
304-
handles, labels = rawdata_axes.get_legend_handles_labels()
305-
legend_labels = [l for l in labels]
306-
legend_handles = [h for h in handles]
307-
if bootstraps_color_by_group is False:
308-
rawdata_axes.legend().set_visible(False)
309-
310305
# Enforce the xtick of rawdata_axes to be 0 and 1 after drawing only one sankey ----> Redundant code
311306
if one_sankey:
312307
rawdata_axes.set_xticks([0, 1])
@@ -373,6 +368,12 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
373368
plot_kwargs=plot_kwargs,
374369
)
375370
# Legend
371+
handles, labels = rawdata_axes.get_legend_handles_labels()
372+
legend_labels = [l for l in labels]
373+
legend_handles = [h for h in handles]
374+
if bootstraps_color_by_group is False:
375+
rawdata_axes.legend().set_visible(False)
376+
376377
if bootstraps_color_by_group is False:
377378
show_legend(legend_labels=legend_labels,
378379
legend_handles=legend_handles,
@@ -383,29 +384,30 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
383384
legend_kwargs=legend_kwargs
384385
)
385386

387+
# Plot aesthetic adjustments.
386388
og_ylim_raw = rawdata_axes.get_ylim()
387389
og_xlim_raw = rawdata_axes.get_xlim()
388390

389391
if float_contrast:
390392
# For Gardner-Altman plots only.
391393
Gardner_Altman_Plot_Aesthetic_Adjustments(effect_size_type=effect_size_type,
392-
plot_data=plot_data,
393-
xvar=xvar,
394-
yvar=yvar,
395-
current_control=current_control,
396-
current_group=current_group,
397-
rawdata_axes=rawdata_axes,
398-
contrast_axes=contrast_axes,
399-
results=results,
400-
current_effsize=current_effsize,
401-
is_paired=is_paired,
402-
one_sankey=one_sankey,
403-
reflines_kwargs=reflines_kwargs,
404-
redraw_axes_kwargs=redraw_axes_kwargs,
405-
swarm_ylim=swarm_ylim,
406-
og_xlim_raw=og_xlim_raw,
407-
og_ylim_raw=og_ylim_raw
408-
)
394+
plot_data=plot_data,
395+
xvar=xvar,
396+
yvar=yvar,
397+
current_control=current_control,
398+
current_group=current_group,
399+
rawdata_axes=rawdata_axes,
400+
contrast_axes=contrast_axes,
401+
results=results,
402+
current_effsize=current_effsize,
403+
is_paired=is_paired,
404+
one_sankey=one_sankey,
405+
reflines_kwargs=reflines_kwargs,
406+
redraw_axes_kwargs=redraw_axes_kwargs,
407+
swarm_ylim=swarm_ylim,
408+
og_xlim_raw=og_xlim_raw,
409+
og_ylim_raw=og_ylim_raw,
410+
)
409411

410412
else:
411413
# For Cumming Plots only.
@@ -426,110 +428,23 @@ def effectsize_df_plotter(effectsize_df, **plot_kwargs):
426428
redraw_axes_kwargs=redraw_axes_kwargs,
427429
ticks_to_skip_contrast=ticks_to_skip_contrast,
428430
)
429-
430-
431-
if show_delta2 or show_mini_meta:
432-
ylim = contrast_axes.get_ylim()
433-
redraw_axes_kwargs["y"] = ylim[0]
434-
x_ticks = contrast_axes.get_xticks()
435-
contrast_axes.hlines(xmin=x_ticks[-2], xmax=x_ticks[-1], **redraw_axes_kwargs)
436-
del redraw_axes_kwargs["y"]
437-
438-
# Set raw axes y-label.
439-
swarm_label = plot_kwargs["swarm_label"]
440-
if swarm_label is None and yvar is None:
441-
swarm_label = "value"
442-
elif swarm_label is None and yvar is not None:
443-
swarm_label = yvar
444-
445-
bar_label = plot_kwargs["bar_label"]
446-
if bar_label is None and effect_size_type != "cohens_h":
447-
bar_label = "proportion of success"
448-
elif bar_label is None and effect_size_type == "cohens_h":
449-
bar_label = "value"
450-
451-
# Place contrast axes y-label.
452-
contrast_label_dict = {
453-
"mean_diff": "mean difference",
454-
"median_diff": "median difference",
455-
"cohens_d": "Cohen's d",
456-
"hedges_g": "Hedges' g",
457-
"cliffs_delta": "Cliff's delta",
458-
"cohens_h": "Cohen's h",
459-
"delta_g": "mean difference",
460-
}
461-
462-
if proportional and effect_size_type != "cohens_h":
463-
default_contrast_label = "proportion difference"
464-
elif effect_size_type == "delta_g":
465-
default_contrast_label = "Hedges' g"
466-
else:
467-
default_contrast_label = contrast_label_dict[effectsize_df.effect_size]
468-
469-
if plot_kwargs["contrast_label"] is None:
470-
if is_paired:
471-
contrast_label = "paired\n{}".format(default_contrast_label)
472-
else:
473-
contrast_label = default_contrast_label
474-
contrast_label = contrast_label.capitalize()
475-
else:
476-
contrast_label = plot_kwargs["contrast_label"]
477-
478-
if plot_kwargs["fontsize_rawylabel"] is not None:
479-
fontsize_rawylabel = plot_kwargs["fontsize_rawylabel"]
480-
if plot_kwargs["fontsize_contrastylabel"] is not None:
481-
fontsize_contrastylabel = plot_kwargs["fontsize_contrastylabel"]
482-
if plot_kwargs["fontsize_delta2label"] is not None:
483-
fontsize_delta2label = plot_kwargs["fontsize_delta2label"]
484-
485-
contrast_axes.set_ylabel(contrast_label, fontsize=fontsize_contrastylabel)
486-
if float_contrast:
487-
contrast_axes.yaxis.set_label_position("right")
488-
489-
# Set the rawdata axes labels appropriately
490-
if not proportional:
491-
rawdata_axes.set_ylabel(swarm_label, fontsize=fontsize_rawylabel)
492-
else:
493-
rawdata_axes.set_ylabel(bar_label, fontsize=fontsize_rawylabel)
494-
rawdata_axes.set_xlabel("")
495-
496-
# Because we turned the axes frame off, we also need to draw back
497-
# the y-spine for both axes.
498-
if not float_contrast:
499-
rawdata_axes.set_xlim(contrast_axes.get_xlim())
500-
og_xlim_raw = rawdata_axes.get_xlim()
501-
rawdata_axes.vlines(
502-
og_xlim_raw[0], og_ylim_raw[0], og_ylim_raw[1], **redraw_axes_kwargs
503-
)
504-
505-
og_xlim_contrast = contrast_axes.get_xlim()
506-
507-
if float_contrast:
508-
xpos = og_xlim_contrast[1]
509-
else:
510-
xpos = og_xlim_contrast[0]
511-
512-
og_ylim_contrast = contrast_axes.get_ylim()
513-
contrast_axes.vlines(
514-
xpos, og_ylim_contrast[0], og_ylim_contrast[1], **redraw_axes_kwargs
515-
)
516-
517-
if show_delta2:
518-
if plot_kwargs["delta2_label"] is not None:
519-
delta2_label = plot_kwargs["delta2_label"]
520-
elif effect_size == "mean_diff":
521-
delta2_label = "delta - delta"
522-
else:
523-
delta2_label = "deltas' g"
524-
delta2_axes = contrast_axes.twinx()
525-
delta2_axes.set_frame_on(False)
526-
delta2_axes.set_ylabel(delta2_label, fontsize=fontsize_delta2label)
527-
og_xlim_delta = contrast_axes.get_xlim()
528-
og_ylim_delta = contrast_axes.get_ylim()
529-
delta2_axes.set_ylim(og_ylim_delta)
530-
delta2_axes.vlines(
531-
og_xlim_delta[1], og_ylim_delta[0], og_ylim_delta[1], **redraw_axes_kwargs
532-
)
431+
432+
# General plotting changes
433+
General_Plot_Aesthetic_Adjustments(show_delta2=show_delta2,
434+
show_mini_meta=show_mini_meta,
435+
contrast_axes=contrast_axes,
436+
redraw_axes_kwargs=redraw_axes_kwargs,
437+
plot_kwargs=plot_kwargs,
438+
yvar=yvar,
439+
effect_size_type=effect_size_type,
440+
proportional=proportional,
441+
effectsize_df=effectsize_df,
442+
is_paired=is_paired,
443+
float_contrast=float_contrast,
444+
rawdata_axes=rawdata_axes,
445+
og_ylim_raw=og_ylim_raw,
446+
effect_size=effect_size,
447+
)
533448

534449
################################################### GRIDKEY WIP
535450
# if gridkey_rows is None, skip everything here

0 commit comments

Comments
 (0)