8000 Handle MOVETO's in Path.interpolated · matplotlib/matplotlib@a53d4f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit a53d4f1

Browse files
committed
Handle MOVETO's in Path.interpolated
1 parent b7e7663 commit a53d4f1

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/matplotlib/path.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,13 +667,27 @@ def intersects_bbox(self, bbox, filled=True):
667667

668668
def interpolated(self, steps):
669669
"""
670-
Return a new path resampled to length N x *steps*.
670+
Return a new path with each segment divided into *steps* parts.
671671
672-
Codes other than `LINETO` are not handled correctly.
672+
Codes other than `LINETO` and `MOVETO` are not handled correctly.
673+
674+
Parameters
675+
----------
676+
steps : int
677+
The number of segments in the new path for each in the original.
678+
679+
Returns
680+
-------
681+
Path
682+
The interpolated path.
673683
"""
674684
if steps == 1:
675685
return self
676686

687+
if self.codes is not None and self.MOVETO in self.codes[1:]:
688+
return self.make_compound_path(
689+
*(p.interpolated(steps) for p in self._iter_connected_components()))
690+
677691
vertices = simple_linear_interpolation(self.vertices, steps)
678692
codes = self.codes
679693
if codes is not None:

lib/matplotlib/tests/test_path.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,21 @@ def test_cleanup_closepoly():
541541
cleaned = p.cleaned(remove_nans=True)
542542
assert len(cleaned) == 1
543543
assert cleaned.codes[0] == Path.STOP
544+
545+
546+
def test_interpolated_moveto():
547+
# Initial path has two subpaths with two LINETOs each
548+
vertices = np.array([[0, 0],
549+
[0, 1],
550+
[1, 2],
551+
[4, 4],
552+
[4, 5],
553+
[5, 5]])
554+
codes = [Path.MOVETO, Path.LINETO, Path.LINETO] * 2
555+
556+
path = Path(vertices, codes)
557+
result = path.interpolated(3)
558+
559+
# Result should have two subpaths with six LINETOs each
560+
expected_subpath_codes = [Path.MOVETO] + [Path.LINETO] * 6
561+
np.testing.assert_array_equal(result.codes, expected_subpath_codes * 2)

0 commit comments

Comments
 (0)
0