statannotations icon indicating copy to clipboard operation
statannotations copied to clipboard

Annotations outside of axis with "loc=inside" set

Open juuf opened this issue 3 years ago • 3 comments

While having the loc=inside option set, is it possible to show the annotations outside of the y-axis limit? I tried to do that by changing the y limits after using statannotations, because else the axis limits are increased, causing the bars to shrink too much (see below).

Unfortunately, by changing the limits afterwards the brackets are not displayed anymore. Do you know what is causing that? image

juuf avatar Nov 07 '22 10:11 juuf

Here is an MWE

import seaborn as sns
from statannotations.Annotator import Annotator

sns.set_theme(style="ticks",rc={"axes.spines.right": False, "axes.spines.top": False}, font_scale=.8)

df = sns.load_dataset("penguins")
ax=sns.barplot(data=df, x="island", y="body_mass_g")

order = ["Torgersen","Biscoe","Dream"]
pairs=[("Torgersen", "Biscoe"), ("Torgersen", "Dream"), ("Biscoe", "Dream")]
annotator = Annotator(ax, pairs, data=df, x="island", y="body_mass_g", order=order)
annotator.configure(test='Mann-Whitney', text_format='star', loc='inside')
annotator.apply_and_annotate()

ax.set_ylim(0, 5.77e3) # change y-limits to not compress error bars too much
ax.set_box_aspect(1)

juuf avatar Nov 09 '22 12:11 juuf

Hi, I encountered the same problem. That's because clipping is not set for loc=inside You can resolve it by adding "clip_on=False" in Annotator.py line 690.

def _plot_line(self, line_x, line_y):
    if self.loc == 'inside':
                self.ax.plot(line_x, line_y, lw=self.line_width, c=self.color, clip_on=False)

amkorb avatar Mar 11 '23 13:03 amkorb

Hi, thanks for pointing this out. It did the trick!

juuf avatar Apr 19 '23 14:04 juuf