Skip to content

Commit

Permalink
Fix multiple moves with pairing (#2817)
Browse files Browse the repository at this point in the history
This was a simple bug
  • Loading branch information
mwaskom authored May 22, 2022
1 parent 1221413 commit 651d06e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
6 changes: 3 additions & 3 deletions seaborn/_core/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
28 changes: 17 additions & 11 deletions seaborn/tests/_core/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -817,29 +817,35 @@ 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"])

assert_frame_equal(long_df, orig_df) # Test data was not mutated

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")
Expand Down

0 comments on commit 651d06e

Please sign in to comment.