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

Skip to content

Commit 48ea00f

Browse files
committed
Support make_compound_path concatenating only empty paths.
Previously `codes[i]` would fail if codes (i.e. the concatenated path) had length zero.
1 parent 22d036e commit 48ea00f

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

lib/matplotlib/path.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -318,29 +318,25 @@ def make_compound_path_from_polys(cls, XY):
318318

319319
@classmethod
320320
def make_compound_path(cls, *args):
321+
r"""
322+
Concatenate a list of `Path`\s into a single `.Path`, removing all `.STOP`\s.
321323
"""
322-
Make a compound path from a list of `Path` objects. Blindly removes
323-
all `Path.STOP` control points.
324-
"""
325-
# Handle an empty list in args (i.e. no args).
326324
if not args:
327325
return Path(np.empty([0, 2], dtype=np.float32))
328-
vertices = np.concatenate([x.vertices for x in args])
326+
vertices = np.concatenate([path.vertices for path in args])
329327
codes = np.empty(len(vertices), dtype=cls.code_type)
330328
i = 0
331329
for path in args:
330+
size = len(path.vertices)
332331
if path.codes is None:
333-
codes[i] = cls.MOVETO
334-
codes[i + 1:i + len(path.vertices)] = cls.LINETO
332+
if size:
333+
codes[i] = cls.MOVETO
334+
codes[i+1:i+size] = cls.LINETO
335335
else:
336-
codes[i:i + len(path.codes)] = path.codes
337-
i += len(path.vertices)
338-
# remove STOP's, since internal STOPs are a bug
339-
not_stop_mask = codes != cls.STOP
340-
vertices = vertices[not_stop_mask, :]
341-
codes = codes[not_stop_mask]
342-
343-
return cls(vertices, codes)
336+
codes[i:i+size] = path.codes
337+
i += size
338+
not_stop_mask = codes != cls.STOP # Remove STOPs, as internal STOPs are a bug.
339+
return cls(vertices[not_stop_mask], codes[not_stop_mask])
344340

345341
def __repr__(self):
346342
return f"Path({self.vertices!r}, {self.codes!r})"

lib/matplotlib/tests/test_path.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ def test_make_compound_path_empty():
205205
# This makes it easier to write generic path based code.
206206
r = Path.make_compound_path()
207207
assert r.vertices.shape == (0, 2)
208+
r2 = Path.make_compound_path(r, r)
209+
assert r2.vertices.shape == (0, 2)
210+
assert r2.codes.shape == (0,)
208211

209212

210213
def test_make_compound_path_stops():

0 commit comments

Comments
 (0)