Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e904fa3

Browse files
authored
Merge pull request #19216 from dstansby/path-extent
Ignore non-draw codes when calculating path extent
2 parents ff7ab9d + 9ccfe60 commit e904fa3

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/matplotlib/path.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,12 @@ def get_extents(self, transform=None, **kwargs):
621621
if self.codes is None:
622622
xys = self.vertices
623623
elif len(np.intersect1d(self.codes, [Path.CURVE3, Path.CURVE4])) == 0:
624-
xys = self.vertices[self.codes != Path.CLOSEPOLY]
624+
# Optimization for the straight line case.
625+
# Instead of iterating through each curve, consider
626+
# each line segment's end-points
627+
# (recall that STOP and CLOSEPOLY vertices are ignored)
628+
xys = self.vertices[np.isin(self.codes,
629+
[Path.MOVETO, Path.LINETO])]
625630
else:
626631
xys = []
627632
for curve, code in self.iter_bezier(**kwargs):

lib/matplotlib/tests/test_path.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ def test_exact_extents(path, extents):
102102
assert np.all(path.get_extents().extents == extents)
103103

104104

105+
@pytest.mark.parametrize('ignored_code', [Path.CLOSEPOLY, Path.STOP])
106+
def test_extents_with_ignored_codes(ignored_code):
107+
# Check that STOP and CLOSEPOLY points are ignored when calculating extents
108+
# of a path with only straight lines
109+
path = Path([[0, 0],
110+
[1, 1],
111+
[2, 2]], [Path.MOVETO, Path.MOVETO, ignored_code])
112+
assert np.all(path.get_extents().extents == (0., 0., 1., 1.))
113+
114+
105115
def test_point_in_path_nan():
106116
box = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]])
107117
p = Path(box)

0 commit comments

Comments
 (0)