Skip to content

Commit 67743c1

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

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``Path.interpolated`` now handles internal ``MOVETO``'s
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
If `.Path` contains internal `~.Path.MOVETO` codes, the `~.Path.interpolated` method
5+
is now applied to each sub-path delineated by those codes.

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 `LINETO` resampled to *steps* `LINETO`'s.
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 `LINETO`'s in the new path for each `LINETO` 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 segments 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 segments with six LINETOs each
560+
expected_segment_codes = [Path.MOVETO] + [Path.LINETO] * 6
561+
np.testing.assert_array_equal(result.codes, expected_segment_codes * 2)

0 commit comments

Comments
 (0)