Skip to content

Commit

Permalink
fix using data=None and hue array
Browse files Browse the repository at this point in the history
  • Loading branch information
getzze committed Jul 1, 2022
1 parent bc0c4b8 commit 6589bf4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
- Fix adding annotations with hue if arrays are provided instead of a Dataframe
and column names as strings (data=None).

## v0.4
### v0.4.4
- The label for Kruskal-Wallis test explicitly states that it is run pairwise
Expand Down
4 changes: 2 additions & 2 deletions statannotations/_Plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,10 @@ def check_plot_is_implemented(plot, engine="seaborn"):

@staticmethod
def fix_and_warn(dodge, hue, plot):
if dodge is False and hue:
if dodge is False and hue is not None:
raise ValueError("`dodge` cannot be False in statannotations.")

if plot in ("swarmplot", 'stripplot') and hue:
if plot in ("swarmplot", 'stripplot') and hue is not None:
if dodge is None:
warnings.warn(
"Implicitly setting dodge to True as it is necessary in "
Expand Down
12 changes: 9 additions & 3 deletions statannotations/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,16 @@ def _check_pairs_in_data_no_hue(pairs: Union[list, tuple],


def _check_pairs_in_data_with_hue(pairs: Union[list, tuple],
data: Union[List[list],
data: Union[None, List[list],
pd.DataFrame] = None,
group_coord: Union[str, list] = None,
hue: str = None) -> set:
group_coord: Union[None, str, list] = None,
hue: Union[None, str, list] = None,
) -> set:

# Arrays provided, not Dataframe
if data is None:
hue_values = set(hue)
return hue_values

x_values = get_x_values(data, group_coord)
seen_group_values = set()
Expand Down
41 changes: 41 additions & 0 deletions tests/test_plotter_no_df.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest
import pandas as pd

from statannotations._Plotter import _SeabornPlotter, IMPLEMENTED_PLOTTERS
import seaborn as sns


class TestPlotterArrays(unittest.TestCase):
def setUp(self) -> None:
# noinspection DuplicatedCode
self.df = pd.DataFrame.from_dict(
{1: {'x': "a", 'y': 15, 'color': 'blue'},
2: {'x': "a", 'y': 16, 'color': 'blue'},
3: {'x': "b", 'y': 17, 'color': 'blue'},
4: {'x': "b", 'y': 18, 'color': 'blue'},
5: {'x': "a", 'y': 15, 'color': 'red'},
6: {'x': "a", 'y': 16, 'color': 'red'},
7: {'x': "b", 'y': 17, 'color': 'red'},
8: {'x': "b", 'y': 18, 'color': 'red'}
}).T
self.df["y"] = self.df["y"].astype(int)

self.plotting = {
"data": None,
"x": self.df["x"],
"y": self.df["y"],
"hue": self.df["color"],
}
self.pairs = [(("a", "blue"), ("a", "red")),
(("b", "blue"), ("b", "red")),
(("a", "blue"), ("b", "blue"))]

def test_seaborn_plots(self):
for plotter in IMPLEMENTED_PLOTTERS["seaborn"]:
if plotter in ("stripplot", "swarmplot"):
plotting = {**self.plotting, "dodge": True}
else:
plotting = self.plotting
ax = getattr(sns, plotter)(**plotting)
_SeabornPlotter(ax, self.pairs, plot=plotter, verbose=False,
**plotting)

0 comments on commit 6589bf4

Please sign in to comment.