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

Skip to content

Commit dc68132

Browse files
committed
Ignore STOP and CLOSPOLY codes when calculating path extents
1 parent 7a0d656 commit dc68132

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/matplotlib/path.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ class Path:
9595
CURVE4: 3,
9696
CLOSEPOLY: 1}
9797

98+
# A list of path codes for which the vertices are ignored
99+
IGNORED_CODES = [STOP, CLOSEPOLY]
100+
98101
def __init__(self, vertices, codes=None, _interpolation_steps=1,
99102
closed=False, readonly=False):
100103
"""
@@ -620,11 +623,20 @@ def get_extents(self, transform=None, **kwargs):
620623
self = transform.transform_path(self)
621624
if self.codes is None:
622625
xys = self.vertices
626+
# Short cut the case with only straight lines
623627
elif len(np.intersect1d(self.codes, [Path.CURVE3, Path.CURVE4])) == 0:
624-
xys = self.vertices[self.codes != Path.CLOSEPOLY]
628+
draw_idxs = np.in1d(self.codes, Path.LINETO)
629+
# Include the start and end point of each line
630+
draw_idxs = draw_idxs | np.roll(draw_idxs, -1)
631+
# Add in commands that aren't ignored
632+
# (in the straight line case here this is only MOVETO)
633+
draw_idxs = draw_idxs | ~np.in1d(self.codes, Path.IGNORED_CODES)
634+
xys = self.vertices[draw_idxs]
625635
else:
626636
xys = []
627637
for curve, code in self.iter_bezier(**kwargs):
638+
if code in Path.IGNORED_CODES:
639+
continue
628640
# places where the derivative is zero can be extrema
629641
_, dzeros = curve.axis_aligned_extrema()
630642
# as can the ends of the curve

lib/matplotlib/tests/test_path.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ 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 caclulating extents
108+
path = Path([[0, 0],
109+
[1, 1],
110+
[2, 2]], [Path.MOVETO, Path.MOVETO, ignored_code])
111+
assert np.all(path.get_extents().extents == (0., 0., 1., 1.))
112+
113+
105114
def test_point_in_path_nan():
106115
box = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]])
107116
p = Path(box)

0 commit comments

Comments
 (0)