Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
54 changes: 34 additions & 20 deletions dabest/misc_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ def initialize_fig(
all_groups_count = np.sum([len(i) for i in dabest_obj.idx])
# Increase the width (vertical layout) or height (horizontal layout) for delta-delta or mini-meta graph
if show_delta2 or show_mini_meta:
all_groups_count += 2
all_groups_count += 1

if horizontal:
frac = 0.3 if is_paired or show_mini_meta else 0.5
Expand Down Expand Up @@ -1217,7 +1217,7 @@ def set_xaxis_ticks_and_lims(
contrast_axes.set_xticks(rawdata_axes.get_xticks())
else:
temp = rawdata_axes.get_xticks()
temp = np.append(temp, [max(temp) + 1, max(temp) + 2])
temp = np.append(temp, [max(temp) + 1])
contrast_axes.set_xticks(temp)

# Lims
Expand All @@ -1227,13 +1227,21 @@ def set_xaxis_ticks_and_lims(

if float_contrast:
contrast_axes.set_xlim(0.5, 1.5)
elif show_delta2 or show_mini_meta:

if show_delta2:
if show_pairs:
rawdata_axes.set_xlim(-0.375, 4.75)
else:
rawdata_axes.set_xlim(-0.5, 4.75)
contrast_axes.set_xlim(rawdata_axes.get_xlim())

elif show_mini_meta:
# Increase the xlim of raw data by 2
temp = rawdata_axes.get_xlim()
if show_pairs:
rawdata_axes.set_xlim(temp[0], temp[1] + 0.5)
else:
rawdata_axes.set_xlim(temp[0], temp[1] + 2)
rawdata_axes.set_xlim(temp[0], temp[1] + 1)
contrast_axes.set_xlim(rawdata_axes.get_xlim())
else:
contrast_axes.set_xlim(rawdata_axes.get_xlim())
Expand Down Expand Up @@ -1529,19 +1537,33 @@ def gardner_altman_adjustments(
def draw_zeroline(
ax : axes.Axes,
horizontal : bool,
reflines_kwargs : dict
reflines_kwargs : dict,
extra_delta : bool,
):
# If 0 lies within the ylim of the contrast axes, draw a zero reference line.
ax_lim = ax.get_xlim() if horizontal else ax.get_ylim()
method = ax.axvline if horizontal else ax.axhline
if extra_delta and not horizontal:
contrast_xlim = [-0.5, 3.4]
delta2_xlim = [3.6, 4.75]

if ax.get_ylim()[0] < ax.get_ylim()[1]:
contrast_lim_low, contrast_lim_high = ax.get_ylim()
else:
contrast_lim_high, contrast_lim_low = ax.get_ylim()

if ax_lim[0] < ax_lim[1]:
contrast_lim_low, contrast_lim_high = ax_lim
if contrast_lim_low < 0 < contrast_lim_high:
ax.hlines(y=0, xmin=contrast_xlim[0], xmax=contrast_xlim[1], **reflines_kwargs)
ax.hlines(y=0, xmin=delta2_xlim[0], xmax=delta2_xlim[1], **reflines_kwargs)
else:
contrast_lim_high, contrast_lim_low = ax_lim
ax_lim = ax.get_xlim() if horizontal else ax.get_ylim()
method = ax.axvline if horizontal else ax.axhline

if ax_lim[0] < ax_lim[1]:
contrast_lim_low, contrast_lim_high = ax_lim
else:
contrast_lim_high, contrast_lim_low = ax_lim

if contrast_lim_low < 0 < contrast_lim_high:
method(0, **reflines_kwargs)
if contrast_lim_low < 0 < contrast_lim_high:
method(0, **reflines_kwargs)

def redraw_independent_spines(
rawdata_axes : axes.Axes,
Expand Down Expand Up @@ -1616,14 +1638,6 @@ def redraw_independent_spines(
ax.set_xlim(xlim)
ax.set_ylim(ylim)
del redraw_axes_kwargs["y"]

# Add x-spine line for delta2/mini meta.
if extra_delta:
ylim = contrast_axes.get_ylim()
redraw_axes_kwargs["y"] = ylim[0]
x_ticks = contrast_axes.get_xticks()
contrast_axes.hlines(xmin=x_ticks[-2], xmax=x_ticks[-1], **redraw_axes_kwargs)
del redraw_axes_kwargs["y"]

def redraw_dependent_spines(
rawdata_axes: axes.Axes,
Expand Down
152 changes: 111 additions & 41 deletions dabest/plot_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ def swarm_contrast_bar_plotter(
if horizontal:
ax_to_plot.add_patch(mpatches.Rectangle((0, max(axes[0].get_yticks())-0.5), diff, 0.5, color='black', **bar_kwargs))
else:
ax_to_plot.add_patch(mpatches.Rectangle((max(axes[0].get_xticks())+2-0.25, 0), 0.5, diff, color='black', **bar_kwargs))
ax_to_plot.add_patch(mpatches.Rectangle((max(axes[0].get_xticks())+1-0.25, 0), 0.5, diff, color='black', **bar_kwargs))

ax_to_plot.set_xlim(og_xlim)
ax_to_plot.set_ylim(og_ylim)
Expand Down Expand Up @@ -1144,8 +1144,8 @@ def delta_text_plotter(
X_Adjust = 0.48 if delta_text_x_location == 'right' else -0.38
X_Adjust += delta_text_x_adjustment
delta_text_x_coordinates = [x+X_Adjust for x in delta_text_x_coordinates]
if show_mini_meta: delta_text_x_coordinates.append(max(swarm_plot_ax.get_xticks())+2+X_Adjust)
if show_delta2: delta_text_x_coordinates.append(max(swarm_plot_ax.get_xticks())+2-0.35)
if show_mini_meta: delta_text_x_coordinates.append(max(swarm_plot_ax.get_xticks())+1+X_Adjust)
if show_delta2: delta_text_x_coordinates.append(max(swarm_plot_ax.get_xticks())+1+X_Adjust)
if show_mini_meta or show_delta2: ticks_to_plot.append(max(ticks_to_plot)+1)

# Collect the Y-coordinates for the delta text
Expand Down Expand Up @@ -1390,6 +1390,7 @@ def plot_minimeta_or_deltadelta_violins(
show_delta2: bool,
plot_kwargs: dict,
horizontal: bool,
show_pairs: bool,
es_marker_kwargs: dict,
es_errorbar_kwargs: dict
):
Expand Down Expand Up @@ -1424,6 +1425,8 @@ def plot_minimeta_or_deltadelta_violins(
Keyword arguments for the plot.
horizontal : bool
If the plot is horizontal.
show_pairs : bool
Whether the data is paired and shown in pairs.
es_marker_kwargs: dict
Keyword arguments for the effectsize marker.
es_errorbar_kwargs: dict
Expand All @@ -1445,12 +1448,12 @@ def extract_curve_data(dabest_object):

if horizontal:
violinplot_kwargs.update({'vert': False, 'widths': 1})
position = max(rawdata_axes.get_yticks()) + 2
position = max(rawdata_axes.get_yticks()) + 1
half = "bottom"
effsize_x, effsize_y = difference, [position]
ci_x, ci_y = [ci_low, ci_high], [position, position]
else:
position = max(rawdata_axes.get_xticks()) + 2
position = max(rawdata_axes.get_xticks()) + 1
half = "right"
effsize_x, effsize_y = [position], difference
ci_x, ci_y = [position, position], [ci_low, ci_high]
Expand Down Expand Up @@ -1489,18 +1492,21 @@ def extract_curve_data(dabest_object):

else:
if show_mini_meta:
contrast_xtick_labels.extend(["", "Weighted Delta"])
if show_pairs:
contrast_xtick_labels.extend(["Weighted\n Delta"])
else:
contrast_xtick_labels.extend(["Weighted Delta"])
elif effect_size == "hedges_g":
contrast_xtick_labels.extend(["", "Deltas' g"])
contrast_xtick_labels.extend(["Deltas' g"])
else:
contrast_xtick_labels.extend(["", "Delta-Delta"])
contrast_xtick_labels.extend(["Delta-Delta"])

# Create the delta-delta axes.
if show_delta2 and not horizontal:
if plot_kwargs["delta2_label"] is not None:
delta2_label = plot_kwargs["delta2_label"]
elif effect_size == "mean_diff":
delta2_label = "Delta - Delta"
delta2_label = "Delta-Delta"
else:
delta2_label = "Deltas' g"
fontsize_delta2label = plot_kwargs["fontsize_delta2label"]
Expand Down Expand Up @@ -1874,29 +1880,51 @@ def gridkey_plotter(
# Add delta-delta or mini_meta details to the table
if show_mini_meta or show_delta2:
if show_delta2:
added_group_name = ["deltas' g"] if effect_size == "hedges_g" else ["delta-delta"]
added_group_name = ["Deltas' g"] if effect_size == "hedges_g" else ["Delta-Delta"]
else:
added_group_name = ["Weighted delta"]
added_group_name = ["Weighted Delta"]
gridkey_rows = added_group_name + gridkey_rows
table_cellcols = [[""]*len(table_cellcols[0])] + table_cellcols

for group_idx, group_vals in enumerate(table_cellcols):
if group_idx == 0:
added_group = ['', gridkey_marker]
elif gridkey_show_es and (group_idx == len(table_cellcols)-1) and not horizontal:
added_delta_effectsize = delta_delta.difference if show_delta2 else mini_meta.difference
added_delta_effectsize_str = np.format_float_positional(
added_delta_effectsize,
precision=2,
sign=True,
trim="k",
min_digits=2,
)
added_group = ['-', added_delta_effectsize_str]
else:
added_group = ['', '']
for n in added_group:
group_vals.append(n)
if not horizontal and show_delta2:
extra_table_cellcols = [[] for i in range(len(table_cellcols))]

for group_idx, group_vals in enumerate(extra_table_cellcols):
if group_idx == 0:
added_group = [gridkey_marker]
elif gridkey_show_es and (group_idx == len(extra_table_cellcols)-1) and not horizontal:
added_delta_effectsize = delta_delta.difference
added_delta_effectsize_str = np.format_float_positional(
added_delta_effectsize,
precision=2,
sign=True,
trim="k",
min_digits=2,
)
added_group = [added_delta_effectsize_str]
else:
added_group = ['']
for n in added_group:
group_vals.append(n)

elif horizontal or show_mini_meta:
for group_idx, group_vals in enumerate(table_cellcols):
if group_idx == 0:
added_group = [gridkey_marker]
elif gridkey_show_es and (group_idx == len(table_cellcols)-1) and not horizontal:
added_delta_effectsize = delta_delta.difference if show_delta2 else mini_meta.difference
added_delta_effectsize_str = np.format_float_positional(
added_delta_effectsize,
precision=2,
sign=True,
trim="k",
min_digits=2,
)
added_group = [added_delta_effectsize_str]
else:
added_group = ['']
for n in added_group:
group_vals.append(n)

# Create the table object
if horizontal:
Expand Down Expand Up @@ -1936,18 +1964,60 @@ def gridkey_plotter(
)
else:
# Plot the table for vertical format
gridkey = ax_to_plot.table(
cellText=table_cellcols,
rowLabels=gridkey_rows,
cellLoc="center",
bbox=[
0,
-len(gridkey_rows) * 0.1 - 0.05,
1,
len(gridkey_rows) * 0.1,
],
**{"alpha": 0.5}
)
if show_mini_meta:
gridkey = ax_to_plot.table(
cellText=table_cellcols,
rowLabels=gridkey_rows,
cellLoc="center",
bbox=[
0,
-len(gridkey_rows) * 0.1 - 0.05,
1,
len(gridkey_rows) * 0.1,
],
**{"alpha": 0.5}
)

elif show_delta2:
gridkey = ax_to_plot.table(
cellText=table_cellcols,
rowLabels=gridkey_rows,
cellLoc="center",
bbox=[
0,
-len(gridkey_rows) * 0.1 - 0.05,
0.75,
len(gridkey_rows) * 0.1,
],
**{"alpha": 0.5}
)

extra_gridkey = ax_to_plot.table(
cellText=extra_table_cellcols,
cellLoc="center",
bbox=[
0.78,
-len(gridkey_rows) * 0.1 - 0.05,
0.15,
len(gridkey_rows) * 0.1,
],
**{"alpha": 0.5}
)

else:
gridkey = ax_to_plot.table(
cellText=table_cellcols,
rowLabels=gridkey_rows,
cellLoc="center",
bbox=[
0,
-len(gridkey_rows) * 0.1 - 0.05,
1,
len(gridkey_rows) * 0.1,
],
**{"alpha": 0.5}
)

# modifies row label cells
for cell in gridkey._cells:
if cell[1] == -1:
Expand Down Expand Up @@ -2133,7 +2203,7 @@ def table_for_horizontal_plots(

### Plot the text
if show_mini_meta or show_delta2:
new_ticks = ticks_to_plot + [max(ticks_to_plot)+2]
new_ticks = ticks_to_plot + [max(ticks_to_plot)+1]
else:
new_ticks = ticks_to_plot.copy()
for i,loc in zip(tab.index, new_ticks):
Expand Down
6 changes: 3 additions & 3 deletions dabest/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ def effectsize_df_plotter(effectsize_df: object, **plot_kwargs) -> matplotlib.fi
show_delta2 = show_delta2,
plot_kwargs = plot_kwargs,
horizontal = horizontal,
show_pairs = show_pairs,
es_marker_kwargs = es_marker_kwargs,
es_errorbar_kwargs = es_errorbar_kwargs
)

## Contrast bars
contrast_bars = plot_kwargs["contrast_bars"]
if contrast_bars:
Expand Down Expand Up @@ -452,7 +452,6 @@ def effectsize_df_plotter(effectsize_df: object, **plot_kwargs) -> matplotlib.fi
proportional = proportional,
horizontal = horizontal,
)

# Plot aesthetic adjustments.
if float_contrast: # For Gardner-Altman (float contrast) plots only.
gardner_altman_adjustments(
Expand All @@ -476,7 +475,8 @@ def effectsize_df_plotter(effectsize_df: object, **plot_kwargs) -> matplotlib.fi
draw_zeroline(
ax = contrast_axes,
horizontal = horizontal,
reflines_kwargs = reflines_kwargs
reflines_kwargs = reflines_kwargs,
extra_delta = True if show_delta2 else False,
)
## Axes independent spine lines
is_gridkey = True if plot_kwargs["gridkey_rows"] is not None else False
Expand Down
Loading