From 651d06e374e17a3c82ea77a29b636b403a50eb18 Mon Sep 17 00:00:00 2001 From: Michael Waskom Date: Sun, 22 May 2022 18:19:08 -0400 Subject: [PATCH] Fix multiple moves with pairing (#2817) This was a simple bug --- seaborn/_core/plot.py | 6 +++--- seaborn/tests/_core/test_plot.py | 28 +++++++++++++++++----------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/seaborn/_core/plot.py b/seaborn/_core/plot.py index 2c735a9287..9af2702c8a 100644 --- a/seaborn/_core/plot.py +++ b/seaborn/_core/plot.py @@ -1104,15 +1104,15 @@ def get_order(var): if move is not None: moves = move if isinstance(move, list) else [move] - for move in moves: + for move_step in moves: move_groupers = [ orient, - *(getattr(move, "by", None) or grouping_properties), + *(getattr(move_step, "by", None) or grouping_properties), *default_grouping_vars, ] order = {var: get_order(var) for var in move_groupers} groupby = GroupBy(order) - df = move(df, groupby, orient) + df = move_step(df, groupby, orient) df = self._unscale_coords(subplots, df, orient) diff --git a/seaborn/tests/_core/test_plot.py b/seaborn/tests/_core/test_plot.py index 35c6a1715d..05394e0347 100644 --- a/seaborn/tests/_core/test_plot.py +++ b/seaborn/tests/_core/test_plot.py @@ -15,7 +15,7 @@ from seaborn._core.plot import Plot from seaborn._core.scales import Nominal, Continuous from seaborn._core.rules import categorical_order -from seaborn._core.moves import Move +from seaborn._core.moves import Move, Shift from seaborn._marks.base import Mark from seaborn._stats.base import Stat from seaborn.external.version import Version @@ -817,12 +817,8 @@ def test_movement(self, long_df): orig_df = long_df.copy(deep=True) - class MockMove(Move): - def __call__(self, data, groupby, orient): - return data.assign(x=data["x"] + 1) - m = MockMark() - Plot(long_df, x="z", y="z").add(m, move=MockMove()).plot() + Plot(long_df, x="z", y="z").add(m, move=Shift(x=1)).plot() assert_vector_equal(m.passed_data[0]["x"], long_df["z"] + 1) assert_vector_equal(m.passed_data[0]["y"], long_df["z"]) @@ -830,16 +826,26 @@ def __call__(self, data, groupby, orient): def test_movement_log_scale(self, long_df): - class MockMove(Move): - def __call__(self, data, groupby, orient): - return data.assign(x=data["x"] - 1) - m = MockMark() Plot( long_df, x="z", y="z" - ).scale(x="log").add(m, move=MockMove()).plot() + ).scale(x="log").add(m, move=Shift(x=-1)).plot() assert_vector_equal(m.passed_data[0]["x"], long_df["z"] / 10) + def test_multi_move(self, long_df): + + m = MockMark() + move_stack = [Shift(1), Shift(2)] + Plot(long_df, x="x", y="y").add(m, move=move_stack).plot() + assert_vector_equal(m.passed_data[0]["x"], long_df["x"] + 3) + + def test_multi_move_with_pairing(self, long_df): + m = MockMark() + move_stack = [Shift(1), Shift(2)] + Plot(long_df, x="x").pair(y=["y", "z"]).add(m, move=move_stack).plot() + for frame in m.passed_data: + assert_vector_equal(frame["x"], long_df["x"] + 3) + def test_methods_clone(self, long_df): p1 = Plot(long_df, "x", "y")