8000 Aternative to #1777. Properly closes the paths returned by clip_to_r… · matplotlib/matplotlib@d56aaa1 · GitHub
[go: up one dir, main page]

Skip to content

Commit d56aaa1

Browse files
committed
Aternative to #1777. Properly closes the paths returned by clip_to_rect and adds a new clip_to_bbox method to Path that returns a Path object. Adds a test that tests clipping compound paths and multiple subsegments.
1 parent 3f6c567 commit d56aaa1

File tree

6 files changed

+573
-13
lines changed

6 files changed

+573
-13
lines changed

lib/matplotlib/path.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from matplotlib._path import point_in_path, get_path_extents, \
1313
point_in_path_collection, get_path_collection_extents, \
1414
path_in_path, path_intersects_path, convert_path_to_polygons, \
15-
cleanup_path, points_in_path
15+
cleanup_path, points_in_path, clip_path_to_rect
1616
from matplotlib.cbook import simple_linear_interpolation, maxdict
1717
from matplotlib import rcParams
1818

@@ -171,22 +171,23 @@ def make_compound_path_from_polys(cls, XY):
171171
def make_compound_path(cls, *args):
172172
"""
173173
(staticmethod) Make a compound path from a list of Path
174-
objects. Only polygons (not curves) are supported.
174+
objects.
175175
"""
176-
for p in args:
177-
assert p.codes is None
178-
179176
lengths = [len(x) for x in args]
180177
total_length = sum(lengths)
181178

182179
vertices = np.vstack([x.vertices for x in args])
183180
vertices.reshape((total_length, 2))
184181

185-
codes = cls.LINETO * np.ones(total_length)
182+
codes = np.ones(total_length, dtype=cls.code_type)
186183
i = 0
187-
for length in lengths:
188-
codes[i] = cls.MOVETO
189-
i += length
184+
for path in args:
185+
if path.codes is None:
186+
codes[i] = cls.MOVETO
187+
codes[i + 1:i + len(path.vertices)] = cls.LINETO
188+
else:
189+
codes[i:i + len(path.codes)] = path.codes
190+
i += len(path.vertices)
190191

191192
return cls(vertices, codes)
192193

@@ -709,6 +710,19 @@ def hatch(cls, hatchpattern, density=6):
709710
cls._hatch_dict[(hatchpattern, density)] = hatch_path
710711
return hatch_path
711712

713+
def clip_to_bbox(self, bbox, inside=True):
714+
"""
715+
Clip the path to the given bounding box.
716+
717+
If *inside* is `True`, clip to the inside of the box, otherwise
718+
to the outside of the box.
719+
"""
720+
# Use make_compound_path_from_polys
721+
verts = clip_path_to_rect(self, bbox, inside)
722+
paths = [Path(poly) for poly in verts]
723+
return self.make_compound_path(*paths)
724+
725+
712726
_get_path_collection_extents = get_path_collection_extents
713727
def get_path_collection_extents(
714728
master_transform, paths, transforms, offsets, offset_transform):
Binary file not shown.
Loading

0 commit comments

Comments
 (0)
0