Skip to content

Annotator causing squished plots, am I missing something? #182

Description

@simbru

I'm finding that sometimes if I have the Annotator set with loc='outside', it will for a lack of a better word "squish" the y axis. I've had similar experiences with horizontally oriented boxplots on the x axis too. I am wondering if there is something that I'm obviously doing wrong?

I cant share the data as its proprietary but I'm sure I could create a minimally reproducible example with demo datasets if its necessary, just let me know. Was hoping perhaps there is something obviously wrong in the code logic below.

Normal (no annotations):
Image
Image

Normal (with annotations):
Image
Squsihed (with annotations):
Image

# data load and prep above, looping through parts of dataset
...
from statannotations.Annotator import Annotator

fig, axes = plt.subplots(1, 4, figsize=(CONTENT_WIDTH_IN*.5, 3 * CM_TO_IN),
                        sharey=True)

# Get colors from custom_palettefor each channel
custom_colors = plotting.custom_palette

# Define statistical comparisons (all pairwise)
stat_pairs = [("off", "on"), ("off", "opp"), ("on", "opp")]

for i, color_channel in enumerate(hue_order):
    color_data = data[data['color_display'] == color_channel]
    channel_color = custom_colors[i]  # Use the specific color for this channel

    sns.boxplot(data=color_data, x='category', y=value_col,
               order=order, color=channel_color, ax=axes[i])

    sns.stripplot(data=color_data, x='category', y=value_col,
                  color='k', ax=axes[i], jitter=True, dodge=True,
                  alpha=.2, marker='.')

    # Add statistical annotations
    annotator = Annotator(axes[i], stat_pairs, data=color_data,
                        x='category', y=value_col, order=order)
    annotator.configure(test='Mann-Whitney', text_format='star', loc='outside',
                    hide_non_significant=True)
    annotator.apply_and_annotate()

    axes[i].set_xlabel('')
    if i == 0:
        axes[i].set_ylabel(f'{metric_name}')
    else:
        axes[i].set_ylabel('')

    # Capitalize x-axis labels (off -> Off, on -> On, opp -> Opp)
    labels = [label.get_text().capitalize() for label in axes[i].get_xticklabels()]
    axes[i].set_xticklabels(labels)
sns.despine()
plt.tight_layout()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions