From 9ccfe60ad071bc6ff3918973c13e08c3f7de12ee Mon Sep 17 00:00:00 2001 From: David Stansby Date: Tue, 5 Jan 2021 14:16:18 +0000 Subject: [PATCH] Ignore STOP and CLOSPOLY codes when calculating path extents --- lib/matplotlib/path.py | 7 ++++++- lib/matplotlib/tests/test_path.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/path.py b/lib/matplotlib/path.py index 1e13168ccd51..a9b5df462491 100644 --- a/lib/matplotlib/path.py +++ b/lib/matplotlib/path.py @@ -621,7 +621,12 @@ def get_extents(self, transform=None, **kwargs): if self.codes is None: xys = self.vertices elif len(np.intersect1d(self.codes, [Path.CURVE3, Path.CURVE4])) == 0: - xys = self.vertices[self.codes != Path.CLOSEPOLY] + # Optimization for the straight line case. + # Instead of iterating through each curve, consider + # each line segment's end-points + # (recall that STOP and CLOSEPOLY vertices are ignored) + xys = self.vertices[np.isin(self.codes, + [Path.MOVETO, Path.LINETO])] else: xys = [] for curve, code in self.iter_bezier(**kwargs): diff --git a/lib/matplotlib/tests/test_path.py b/lib/matplotlib/tests/test_path.py index 2c4844b47f37..8bd7777eca0d 100644 --- a/lib/matplotlib/tests/test_path.py +++ b/lib/matplotlib/tests/test_path.py @@ -102,6 +102,16 @@ def test_exact_extents(path, extents): assert np.all(path.get_extents().extents == extents) +@pytest.mark.parametrize('ignored_code', [Path.CLOSEPOLY, Path.STOP]) +def test_extents_with_ignored_codes(ignored_code): + # Check that STOP and CLOSEPOLY points are ignored when calculating extents + # of a path with only straight lines + path = Path([[0, 0], + [1, 1], + [2, 2]], [Path.MOVETO, Path.MOVETO, ignored_code]) + assert np.all(path.get_extents().extents == (0., 0., 1., 1.)) + + def test_point_in_path_nan(): box = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]) p = Path(box)