trevismd / statannotations

add statistical significance annotations on seaborn plots. Further development of statannot, with bugfixes, new features, and a different API.
Other
624 stars 67 forks source link

Annotations outside of axis with "loc=inside" set #91

Open juuf opened 1 year ago

juuf commented 1 year ago

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 commented 1 year ago

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)
amkorb commented 1 year ago

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)
juuf commented 1 year ago

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