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

Ability to add custom (manual) p_values for box plot with hue #44

Closed HamishForrest closed 2 years ago

HamishForrest commented 2 years ago

Hi,

It seems likeadding custom (manual) p_values for box plot with hue is not possible. I cannot see an example of it anywhere and it would be very useful please! Thanks!

I guess it would look something like:

p_values = [0.1, 0.2, 0.001]
annot = Annotator(ax = ax, pairs = pairs, x = "Age", hue = "Sex", y = "value")
annot.new_plot(data = data)
annot.configure(loc='outside', test = None).set_pvalues(p_values)
trevismd commented 2 years ago

Hello, It does work fine but it may not be clear how without demo. Here is one based on your example (with fake data of course):

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

from statannotations.Annotator import Annotator

data = [
    {"Age": 5, "Sex": "F", "value": 3},
    {"Age": 5, "Sex": "F", "value": 8},
    {"Age": 5, "Sex": "F", "value": 6},
    {"Age": 8, "Sex": "F", "value": 4},
    {"Age": 8, "Sex": "F", "value": 11},
    {"Age": 8, "Sex": "F", "value": 14},
    {"Age": 8, "Sex": "M", "value": 10},
    {"Age": 5, "Sex": "M", "value": 3},
    {"Age": 5, "Sex": "M", "value": 3},
    {"Age": 5, "Sex": "M", "value": 9},
    {"Age": 8, "Sex": "M", "value": 12},
    {"Age": 5, "Sex": "O", "value": 8},
    {"Age": 5, "Sex": "O", "value": 8},
    {"Age": 5, "Sex": "O", "value": 6},
    {"Age": 8, "Sex": "O", "value": 6},
    {"Age": 8, "Sex": "O", "value": 3},
    {"Age": 8, "Sex": "O", "value": 2},
]

data = pd.DataFrame(data)
plot_params = {
    "x": "Age",
    "y": "value",
    "hue": "Sex",
    "data": data
}

ax = sns.boxplot(**plot_params)
pairs = [
    [(5, "M"), (5, "F")],
    [(5, "F"), (5, "O")],
    [(8, "M"), (8, "O")],
]
p_values = [0.1, 0.2, 0.001]
annot = Annotator(ax=ax, pairs=pairs, **plot_params)
annot.configure(loc='outside').set_pvalues_and_annotate(p_values)
plt.savefig('example_hue_custom.png', dpi=300, bbox_inches='tight')

Which makes example_hue_custom

trevismd commented 2 years ago

@HamishForrest, I'm closing this but please do not hesitate to comment if something isn't clear.

HamishForrest commented 2 years ago

Thanks for your help! Your solution worked great.

Happy continue using this repository it has been really useful.