1

Goal: I am trying to insert statistical annotations inside a seaborn figure-level plot with NaN values without showing datapoints for NaN values.

What I tried: I noticed that inserting statistical annotations with statannotations.Annotator.Annotator resulted in a ValueError: output array is read-only if there are NaN values in the dataframe. So I tried to filter all None objects out and then used statannotations.Annotator.Annotator. However, this just worked when the annotation is located outside the figure. I would like to insert the annotation inside the figure with same layout as shown below:

import seaborn as sns
from statannotations.Annotator import Annotator
%matplotlib inline
import math


tips = sns.load_dataset("tips")
tips.loc[tips['sex'] == "Male", 'total_bill'] = math.nan

args = dict(x="sex", y="total_bill", data=tips, hue="smoker", hue_order=["Yes","No"], order=['Male', 'Female'])

g = sns.catplot(edgecolor="black", errcolor="black", errwidth=1.5, capsize = 0.1, height=4, aspect=.7,alpha=0.5, kind="bar", ci = "sd", row="time", **args)
g.map(sns.stripplot, args["x"], args["y"], args["hue"], hue_order=args["hue_order"], order=args["order"], palette=sns.color_palette(), dodge=True, alpha=0.6, ec='k', linewidth=1)
g.set_titles(row_template="{row_name}")

pairs = [
(("Female", "Yes"), ("Female", "No"))
]

for ax in g.axes.flat:
    ax_unfiltered = ax.get_children
    ax_filter = list(filter(lambda x: x is not None, [el.get_clip_box() for el in ax.get_children()]))
    ax.get_children = lambda: ax_filter

    annot = Annotator(ax, pairs, **args)
    annot.configure(test='Mann-Whitney', text_format='star', loc="outside", verbose=2,)


    ax.get_children = ax_unfiltered
    annot.apply_test().annotate()

Statistical Annotation outside


If I replace NaN values with 0, I can insert statistical annotations inside the figure. However, this also results in datapoints for NaN values. I would like to have a figure as shown below with no datapoints for NaN values:

import seaborn as sns
from statannotations.Annotator import Annotator
%matplotlib inline


tips = sns.load_dataset("tips")
tips.loc[tips['sex'] == "Male", 'total_bill'] = 0

args = dict(x="sex", y="total_bill", data=tips, hue="smoker", hue_order=["Yes","No"], order=['Male', 'Female'])

g = sns.catplot(edgecolor="black", errcolor="black", errwidth=1.5, capsize = 0.1, height=4, aspect=.7,alpha=0.5, kind="bar", ci = "sd", row="time", **args)
g.map(sns.stripplot, args["x"], args["y"], args["hue"], hue_order=args["hue_order"], order=args["order"], palette=sns.color_palette(), dodge=True, alpha=0.6, ec='k', linewidth=1)

pairs = [
(("Female", "Yes"), ("Female", "No"))
]

for ax in g.axes.flat:
    annot = Annotator(ax, pairs, **args)
    annot.configure(test='Mann-Whitney', text_format='simple', loc='inside', verbose=2)
    annot.apply_test().annotate()

Statistical Annotation with datapoints for NaN The code above is based on @M. Sch.

Question How do I add statistical annotations inside the figure if there are NaN values?

Alex Schubert
  • 67
  • 2
  • 7

0 Answers0