statannotations icon indicating copy to clipboard operation
statannotations copied to clipboard

Cannot configure annotator for plot when groups are in y axis

Open jonpedros opened this issue 2 years ago • 1 comments

I think I might be doing something wrong here, but...

import seaborn as sns
from statannotations.Annotator import Annotator

iris = sns.load_dataset("iris")
iris = pd.melt(iris, "species", var_name="measurement")
pair_list = [(('sepal_length', 'setosa'),
              ('sepal_length', 'versicolor'),
              ('sepal_length', 'virginica')),
             (('sepal_width', 'setosa'),
              ('sepal_width', 'versicolor'),
              ('sepal_width', 'virginica')),
             (('petal_length', 'setosa'),
              ('petal_length', 'versicolor'),
              ('petal_length', 'virginica')),
             (('petal_width', 'setosa'),
              ('petal_width', 'versicolor'),
              ('petal_width', 'virginica'))]
petal_ax = sns.pointplot(
    data=iris, x="value", y="measurement", hue="species",
    join=False, dodge=.8 - .8 / 3, palette="dark",
    markers="d", scale=.75, errorbar=None
)
annotator = Annotator(ax=petal_ax, 
                        pairs=pair_list, 
                        data=iris, 
                        y="measurement",
                        x="value",
                        hue="species", 
                        order=['sepal_length', 'sepal_width', 'petal_length', 'petal_width'],
                        hue_order=['setosa', 'versicolor', 'virginica'])
annotator.configure(test='Mann-Whitney', text_format='star', loc='outside')
annotator.apply_and_annotate()

Returns

ValueError                                Traceback (most recent call last)
[c:\Users\systemses\Downloads\pyControl\running_analysis](file:///C:/Users/systemses/Downloads/pyControl/running_analysis)[rand].ipynb Cell 9 in 2
      [3](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=2) pair_list = [(('sepal_length', 'setosa'),
      [4](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=3)               ('sepal_length', 'versicolor'),
      [5](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=4)               ('sepal_length', 'virginica')),
   (...)
     [13](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=12)               ('petal_width', 'versicolor'),
     [14](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=13)               ('petal_width', 'virginica'))]
     [15](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=14) petal_ax = sns.pointplot(
     [16](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=15)     data=iris, x="value", y="measurement", hue="species",
     [17](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=16)     join=False, dodge=.8 - .8 / 3, palette="dark",
     [18](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=17)     markers="d", scale=.75, errorbar=None
     [19](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=18) )
---> [20](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=19) annotator = Annotator(ax=petal_ax, 
     [21](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=20)                         pairs=pair_list, 
     [22](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=21)                         data=iris, 
     [23](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=22)                         y="measurement",
     [24](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=23)                         x="value",
     [25](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=24)                         hue="species", 
     [26](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=25)                         order=['sepal_length', 'sepal_width', 'petal_length', 'petal_width'],
     [27](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=26)                         hue_order=['setosa', 'versicolor', 'virginica'])
     [28](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=27) annotator.configure(test='Mann-Whitney', text_format='star', loc='outside')
     [29](vscode-notebook-cell:/c%3A/Users/systemses/Downloads/pyControl/running_analysis%5Brand%5D.ipynb#X42sZmlsZQ%3D%3D?line=28) annotator.apply_and_annotate()
...
---> 63     raise ValueError(f"Missing x value(s) "
     64                      f"`{render_collection(unmatched_in_order)}` in {x} "
     65                      f"(specified in `order`)")

ValueError: Missing x value(s) `"sepal_width", "petal_length", "sepal_length", "petal_width"` in value (specified in `order`)

jonpedros avatar Mar 06 '23 09:03 jonpedros

Hello! I can see that your pair_list is a list of 3-tuples, which isn't the expected format to define the groups to compare. It should look like this instead:

pair_list = [
    (('sepal_length', 'setosa'), ('sepal_length', 'versicolor')),
    (('sepal_length', 'virginica')), ('sepal_width', 'setosa')),
    (('sepal_width', 'versicolor'), ('sepal_width', 'virginica')),
    (('petal_length', 'setosa'), ('petal_length', 'versicolor')),
    ...
]

trevismd avatar Mar 08 '23 08:03 trevismd