diff --git a/lib/matplotlib/tests/test_transforms.py b/lib/matplotlib/tests/test_transforms.py index d869240adaec..3af9af68ec94 100644 --- a/lib/matplotlib/tests/test_transforms.py +++ b/lib/matplotlib/tests/test_transforms.py @@ -700,6 +700,18 @@ def test_contains_branch(self): assert x is sx is False assert y is sy is True + # A branch that lives in the first child of a composite, or that spans + # the boundary between its two children, must be reported in both + # dimensions, consistently with contains_branch (issue #32099). + assert self.stack2.contains_branch(self.stack2_subset) + assert (self.stack2.contains_branch_separately(self.stack2_subset) + == (True, True)) + assert (self.stack1.contains_branch_separately(self.ta2 + self.ta3) + == (True, True)) + # A transform that is not a branch stays False in both dimensions. + assert (self.stack2.contains_branch_separately(self.tn1) + == (False, False)) + def test_affine_simplification(self): # tests that a transform stack only calls as much is absolutely # necessary "non-affine" allowing the best possible optimization with diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index 2b46deae43dd..d61fefa2b221 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -2454,7 +2454,18 @@ def contains_branch_separately(self, other_transform): 'transforms with 2 output dimensions') if self == other_transform: return (True, True) - return self._b.contains_branch_separately(other_transform) + # The second (suffix) child may report per-dimension results when it + # is a blended transform, so query it separately first. + x, y = self._b.contains_branch_separately(other_transform) + if not (x and y): + # Otherwise the branch may lie in the first child or span the + # boundary between the two children. Such a branch is a suffix of + # the whole composite and therefore matches in both dimensions, so + # fall back to the (non-per-dimension) whole-transform check. + in_branch = self.contains_branch(other_transform) + x = x or in_branch + y = y or in_branch + return (x, y) depth = property(lambda self: self._a.depth + self._b.depth) is_affine = property(lambda self: self._a.is_affine and self._b.is_affine)