Skip to content

Commit

Permalink
Further additions to horizontal plot functionality part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
JAnns98 committed Sep 26, 2024
1 parent 9f2cced commit f7bf14f
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 187 deletions.
177 changes: 97 additions & 80 deletions dabest/misc_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,43 +1029,7 @@ def Cumming_Plot_Aesthetic_Adjustments(plot_kwargs, show_delta2, effect_size_typ
end_tick = rightend_ticks_raw[k]
ax.vlines(ymin=start_tick, ymax=end_tick, **redraw_axes_kwargs)
ax.set_xlim(xlim)
del redraw_axes_kwargs["x"]


if not proportional:
temp_length = [(len(i) - 1) for i in idx]
else:
temp_length = [(len(i) - 1) * 2 - 1 for i in idx]
if two_col_sankey:
rightend_ticks_contrast = np.array(
[len(i) - 2 for i in idx]
) + np.array(ticks_to_start_twocol_sankey)
elif proportional and is_paired is not None:
rightend_ticks_contrast = np.array(
[len(i) - 1 for i in idx]
) + np.array(ticks_to_skip)
else:
rightend_ticks_contrast = np.array(temp_length) + np.array(
ticks_to_skip_contrast
)

for ax in [contrast_axes]:
sns.despine(ax=ax, left=True)
ylim = ax.get_ylim()
xlim = ax.get_xlim()
redraw_axes_kwargs["x"] = xlim[0]

if two_col_sankey:
for k, start_tick in enumerate(ticks_to_start_twocol_sankey):
end_tick = rightend_ticks_contrast[k]
ax.vlines(ymin=start_tick, ymax=end_tick, **redraw_axes_kwargs)
else:
for k, start_tick in enumerate(ticks_to_skip_contrast):
end_tick = rightend_ticks_contrast[k]
ax.vlines(ymin=start_tick, ymax=end_tick, **redraw_axes_kwargs)

ax.set_xlim(xlim)
del redraw_axes_kwargs["x"]
del redraw_axes_kwargs["x"]
else:
# Compute the end of each x-axes line.
if two_col_sankey:
Expand All @@ -1077,7 +1041,7 @@ def Cumming_Plot_Aesthetic_Adjustments(plot_kwargs, show_delta2, effect_size_typ
ticks_to_skip
)

for ax in [rawdata_axes, contrast_axes]:
for ax in [rawdata_axes]:
sns.despine(ax=ax, left=True)

ylim = ax.get_ylim()
Expand All @@ -1095,6 +1059,12 @@ def Cumming_Plot_Aesthetic_Adjustments(plot_kwargs, show_delta2, effect_size_typ

ax.set_xlim(xlim)
del redraw_axes_kwargs["x"]

# Remove y ticks and labels from the contrast axes.
sns.despine(ax=contrast_axes, left=True)
contrast_axes.set_yticks([])
contrast_axes.set_yticklabels([])

else:
contrast_axes_ylim = contrast_axes.get_ylim()
if contrast_axes_ylim[0] < contrast_axes_ylim[1]:
Expand Down Expand Up @@ -1201,9 +1171,10 @@ def Cumming_Plot_Aesthetic_Adjustments(plot_kwargs, show_delta2, effect_size_typ

def General_Plot_Aesthetic_Adjustments(show_delta2, show_mini_meta, contrast_axes, redraw_axes_kwargs, plot_kwargs,
yvar, effect_size_type, proportional, effectsize_df, is_paired, float_contrast,
rawdata_axes, og_ylim_raw, effect_size):
rawdata_axes, og_ylim_raw, effect_size, horizontal=False):

if show_delta2 or show_mini_meta:

if (show_delta2 or show_mini_meta) and not horizontal:
ylim = contrast_axes.get_ylim()
redraw_axes_kwargs["y"] = ylim[0]
x_ticks = contrast_axes.get_xticks()
Expand Down Expand Up @@ -1257,51 +1228,97 @@ def General_Plot_Aesthetic_Adjustments(show_delta2, show_mini_meta, contrast_axe
if plot_kwargs["fontsize_delta2label"] is not None:
fontsize_delta2label = plot_kwargs["fontsize_delta2label"]

contrast_axes.set_ylabel(contrast_label, fontsize=fontsize_contrastylabel)
if float_contrast:
contrast_axes.yaxis.set_label_position("right")
if horizontal:

contrast_axes.set_xlabel(contrast_label, fontsize=fontsize_contrastylabel)
# Set the rawdata axes labels appropriately
if not proportional:
rawdata_axes.set_xlabel(swarm_label, fontsize=fontsize_rawylabel)
else:
rawdata_axes.set_xlabel(bar_label, fontsize=fontsize_rawylabel)
rawdata_axes.set_ylabel("")

# Set the rawdata axes labels appropriately
if not proportional:
rawdata_axes.set_ylabel(swarm_label, fontsize=fontsize_rawylabel)
else:
rawdata_axes.set_ylabel(bar_label, fontsize=fontsize_rawylabel)
rawdata_axes.set_xlabel("")

# Because we turned the axes frame off, we also need to draw back
# the y-spine for both axes.
if not float_contrast:
rawdata_axes.set_xlim(contrast_axes.get_xlim())
og_xlim_raw = rawdata_axes.get_xlim()
rawdata_axes.vlines(
og_xlim_raw[0], og_ylim_raw[0], og_ylim_raw[1], **redraw_axes_kwargs
)
# Because we turned the axes frame off, we also need to draw back
# the x-spine for both axes.
rawdata_axes.set_ylim(contrast_axes.get_ylim())
og_xlim_raw = og_ylim_raw
new_ylim_raw = rawdata_axes.get_ylim()

rawdata_axes.hlines(
new_ylim_raw[1], og_xlim_raw[0], og_xlim_raw[1], **redraw_axes_kwargs
)

og_xlim_contrast = contrast_axes.get_xlim()
og_ylim_contrast = contrast_axes.get_ylim()
ypos = og_ylim_contrast[1]

og_xlim_contrast = contrast_axes.get_xlim()
contrast_axes.hlines(
ypos, og_xlim_contrast[0], og_xlim_contrast[1], **redraw_axes_kwargs
)

if show_delta2:
if plot_kwargs["delta2_label"] is not None:
delta2_label = plot_kwargs["delta2_label"]
elif effect_size == "mean_diff":
delta2_label = "delta - delta"
else:
delta2_label = "deltas' g"
raise NotImplementedError("Delta2 is not yet supported for horizontal plots.")

swarm_ylim = plot_kwargs["swarm_ylim"]
contrast_ylim = plot_kwargs["contrast_ylim"]
if swarm_ylim is None:
swarm_ylim = rawdata_axes.get_ylim()
rawdata_axes.set_ylim(swarm_ylim[1], swarm_ylim[0])
if plot_kwargs['contrast_ylim'] is None:
contrast_ylim = contrast_axes.get_ylim()
contrast_axes.set_ylim(contrast_ylim[1], contrast_ylim[0])

if float_contrast:
xpos = og_xlim_contrast[1]
else:
xpos = og_xlim_contrast[0]
contrast_axes.set_ylabel(contrast_label, fontsize=fontsize_contrastylabel)
if float_contrast:
contrast_axes.yaxis.set_label_position("right")
# Set the rawdata axes labels appropriately
if not proportional:
rawdata_axes.set_ylabel(swarm_label, fontsize=fontsize_rawylabel)
else:
rawdata_axes.set_ylabel(bar_label, fontsize=fontsize_rawylabel)
rawdata_axes.set_xlabel("")

# Because we turned the axes frame off, we also need to draw back
# the y-spine for both axes.
if not float_contrast:
rawdata_axes.set_xlim(contrast_axes.get_xlim())
og_xlim_raw = rawdata_axes.get_xlim()
rawdata_axes.vlines(
og_xlim_raw[0], og_ylim_raw[0], og_ylim_raw[1], **redraw_axes_kwargs
)

og_ylim_contrast = contrast_axes.get_ylim()
contrast_axes.vlines(
xpos, og_ylim_contrast[0], og_ylim_contrast[1], **redraw_axes_kwargs
)
og_xlim_contrast = contrast_axes.get_xlim()

if show_delta2:
if plot_kwargs["delta2_label"] is not None:
delta2_label = plot_kwargs["delta2_label"]
elif effect_size == "mean_diff":
delta2_label = "delta - delta"
if float_contrast:
xpos = og_xlim_contrast[1]
else:
delta2_label = "deltas' g"
delta2_axes = contrast_axes.twinx()
delta2_axes.set_frame_on(False)
delta2_axes.set_ylabel(delta2_label, fontsize=fontsize_delta2label)
og_xlim_delta = contrast_axes.get_xlim()
og_ylim_delta = contrast_axes.get_ylim()
delta2_axes.set_ylim(og_ylim_delta)
delta2_axes.vlines(
og_xlim_delta[1], og_ylim_delta[0], og_ylim_delta[1], **redraw_axes_kwargs
xpos = og_xlim_contrast[0]

og_ylim_contrast = contrast_axes.get_ylim()
contrast_axes.vlines(
xpos, og_ylim_contrast[0], og_ylim_contrast[1], **redraw_axes_kwargs
)

if show_delta2:
if plot_kwargs["delta2_label"] is not None:
delta2_label = plot_kwargs["delta2_label"]
elif effect_size == "mean_diff":
delta2_label = "delta - delta"
else:
delta2_label = "deltas' g"
delta2_axes = contrast_axes.twinx()
delta2_axes.set_frame_on(False)
delta2_axes.set_ylabel(delta2_label, fontsize=fontsize_delta2label)
og_xlim_delta = contrast_axes.get_xlim()
og_ylim_delta = contrast_axes.get_ylim()
delta2_axes.set_ylim(og_ylim_delta)
delta2_axes.vlines(
og_xlim_delta[1], og_ylim_delta[0], og_ylim_delta[1], **redraw_axes_kwargs
)
42 changes: 29 additions & 13 deletions dabest/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ def effectsize_df_plotter_horizontal(effectsize_df, **plot_kwargs):
set_xaxis_ticks_and_lims,
show_legend,
Cumming_Plot_Aesthetic_Adjustments,
General_Plot_Aesthetic_Adjustments,

)
from .plot_tools import (
Expand All @@ -637,6 +638,8 @@ def effectsize_df_plotter_horizontal(effectsize_df, **plot_kwargs):
# tries to get from slopegraph.
logging.disable(logging.WARNING)



# Save rcParams that I will alter, so I can reset back.
original_rcParams = {}
_changed_rcParams = ["axes.grid"]
Expand All @@ -653,7 +656,8 @@ def effectsize_df_plotter_horizontal(effectsize_df, **plot_kwargs):
effectsize_df=effectsize_df,
plot_kwargs=plot_kwargs
)

if plot_kwargs['horizontal']:
float_contrast = False

(swarmplot_kwargs, barplot_kwargs, sankey_kwargs, violinplot_kwargs,
slopegraph_kwargs, reflines_kwargs, legend_kwargs, group_summary_kwargs, redraw_axes_kwargs,
Expand Down Expand Up @@ -1010,18 +1014,30 @@ def effectsize_df_plotter_horizontal(effectsize_df, **plot_kwargs):
horizontal=True,
)







if swarm_ylim is None:
swarm_ylim = rawdata_axes.get_ylim()
rawdata_axes.set_ylim(swarm_ylim[1], swarm_ylim[0])
if plot_kwargs['contrast_ylim'] is None:
contrast_ylim = contrast_axes.get_ylim()
contrast_axes.set_ylim(contrast_ylim[1], contrast_ylim[0])
# General plotting changes
General_Plot_Aesthetic_Adjustments(
show_delta2=show_delta2,
show_mini_meta=show_mini_meta,
contrast_axes=contrast_axes,
redraw_axes_kwargs=redraw_axes_kwargs,
plot_kwargs=plot_kwargs,
yvar=yvar,
effect_size_type=effect_size_type,
proportional=proportional,
effectsize_df=effectsize_df,
is_paired=is_paired,
float_contrast=float_contrast,
rawdata_axes=rawdata_axes,
og_ylim_raw=og_xlim_raw if plot_kwargs['horizontal'] else og_ylim_raw,
effect_size=effect_size,
horizontal=True,
)
# if swarm_ylim is None:
# swarm_ylim = rawdata_axes.get_ylim()
# rawdata_axes.set_ylim(swarm_ylim[1], swarm_ylim[0])
# if plot_kwargs['contrast_ylim'] is None:
# contrast_ylim = contrast_axes.get_ylim()
# contrast_axes.set_ylim(contrast_ylim[1], contrast_ylim[0])


# Make sure no stray ticks appear!
Expand Down
Loading

0 comments on commit f7bf14f

Please sign in to comment.