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

Skip to content

Commit 386d615

Browse files
authored
Merge pull request #23368 from anntzer/mc
Add a helper to generate closed paths.
2 parents 79b7f25 + e994b58 commit 386d615

File tree

10 files changed

+55
-63
lines changed

10 files changed

+55
-63
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4031,9 +4031,8 @@ def do_plot(xs, ys, **kwargs):
40314031
return self.plot(*[xs, ys][maybe_swap], **kwargs)[0]
40324032

40334033
def do_patch(xs, ys, **kwargs):
4034-
path = mpath.Path(
4035-
# Last (0, 0) vertex has a CLOSEPOLY code and is thus ignored.
4036-
np.column_stack([[*xs, 0], [*ys, 0]][maybe_swap]), closed=True)
4034+
path = mpath.Path._create_closed(
4035+
np.column_stack([xs, ys][maybe_swap]))
40374036
patch = mpatches.PathPatch(path, **kwargs)
40384037
self.add_artist(patch)
40394038
return patch

lib/matplotlib/collections.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,11 +1217,7 @@ def set_verts(self, verts, closed=True):
12171217
self._paths = []
12181218
for xy in verts:
12191219
if len(xy):
1220-
if isinstance(xy, np.ma.MaskedArray):
1221-
xy = np.ma.concatenate([xy, xy[:1]])
1222-
else:
1223-
xy = np.concatenate([xy, xy[:1]])
1224-
self._paths.append(mpath.Path(xy, closed=True))
1220+
self._paths.append(mpath.Path._create_closed(xy))
12251221
else:
12261222
self._paths.append(mpath.Path(xy))
12271223

lib/matplotlib/colorbar.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ def _set_ticks_on_axis_warn(*args, **kwargs):
195195
class _ColorbarSpine(mspines.Spine):
196196
def __init__(self, axes):
197197
self._ax = axes
198-
super().__init__(axes, 'colorbar',
199-
mpath.Path(np.empty((0, 2)), closed=True))
198+
super().__init__(axes, 'colorbar', mpath.Path(np.empty((0, 2))))
200199
mpatches.Patch.set_transform(self, axes.transAxes)
201200

202201
def get_window_extent(self, renderer=None):

lib/matplotlib/markers.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -571,15 +571,13 @@ def _set_pixel(self):
571571
self._transform = Affine2D().translate(-0.49999, -0.49999)
572572
self._snap_threshold = None
573573

574-
_triangle_path = Path([[0, 1], [-1, -1], [1, -1], [0, 1]], closed=True)
574+
_triangle_path = Path._create_closed([[0, 1], [-1, -1], [1, -1]])
575575
# Going down halfway looks to small. Golden ratio is too far.
576-
_triangle_path_u = Path([[0, 1], [-3/5, -1/5], [3/5, -1/5], [0, 1]],
577-
closed=True)
578-
_triangle_path_d = Path(
579-
[[-3/5, -1/5], [3/5, -1/5], [1, -1], [-1, -1], [-3/5, -1/5]],
580-
closed=True)
581-
_triangle_path_l = Path([[0, 1], [0, -1], [-1, -1], [0, 1]], closed=True)
582-
_triangle_path_r = Path([[0, 1], [0, -1], [1, -1], [0, 1]], closed=True)
576+
_triangle_path_u = Path._create_closed([[0, 1], [-3/5, -1/5], [3/5, -1/5]])
577+
_triangle_path_d = Path._create_closed(
578+
[[-3/5, -1/5], [3/5, -1/5], [1, -1], [-1, -1]])
579+
_triangle_path_l = Path._create_closed([[0, 1], [0, -1], [-1, -1]])
580+
_triangle_path_r = Path._create_closed([[0, 1], [0, -1], [1, -1]])
583581

584582
def _set_triangle(self, rot, skip):
585583
self._transform = Affine2D().scale(0.5).rotate_deg(rot)
@@ -901,14 +899,12 @@ def _set_x(self):
901899
self._filled = False
902900
self._path = self._x_path
903901

904-
_plus_filled_path = Path(
905-
np.array([(-1, -3), (+1, -3), (+1, -1), (+3, -1), (+3, +1), (+1, +1),
906-
(+1, +3), (-1, +3), (-1, +1), (-3, +1), (-3, -1), (-1, -1),
907-
(-1, -3)]) / 6, closed=True)
908-
_plus_filled_path_t = Path(
909-
np.array([(+3, 0), (+3, +1), (+1, +1), (+1, +3),
910-
(-1, +3), (-1, +1), (-3, +1), (-3, 0),
911-
(+3, 0)]) / 6, closed=True)
902+
_plus_filled_path = Path._create_closed(np.array([
903+
(-1, -3), (+1, -3), (+1, -1), (+3, -1), (+3, +1), (+1, +1),
904+
(+1, +3), (-1, +3), (-1, +1), (-3, +1), (-3, -1), (-1, -1)]) / 6)
905+
_plus_filled_path_t = Path._create_closed(np.array([
906+
(+3, 0), (+3, +1), (+1, +1), (+1, +3),
907+
(-1, +3), (-1, +1), (-3, +1), (-3, 0)]) / 6)
912908

913909
def _set_plus_filled(self):
914910
self._transform = Affine2D()
@@ -924,15 +920,12 @@ def _set_plus_filled(self):
924920
{'top': 0, 'left': 90, 'bottom': 180, 'right': 270}[fs])
925921
self._alt_transform = self._transform.frozen().rotate_deg(180)
926922

927-
_x_filled_path = Path(
928-
np.array([(-1, -2), (0, -1), (+1, -2), (+2, -1), (+1, 0), (+2, +1),
929-
(+1, +2), (0, +1), (-1, +2), (-2, +1), (-1, 0), (-2, -1),
930-
(-1, -2)]) / 4,
931-
closed=True)
932-
_x_filled_path_t = Path(
933-
np.array([(+1, 0), (+2, +1), (+1, +2), (0, +1),
934-
(-1, +2), (-2, +1), (-1, 0), (+1, 0)]) / 4,
935-
closed=True)
923+
_x_filled_path = Path._create_closed(np.array([
924+
(-1, -2), (0, -1), (+1, -2), (+2, -1), (+1, 0), (+2, +1),
925+
(+1, +2), (0, +1), (-1, +2), (-2, +1), (-1, 0), (-2, -1)]) / 4)
926+
_x_filled_path_t = Path._create_closed(np.array([
927+
(+1, 0), (+2, +1), (+1, +2), (0, +1),
928+
(-1, +2), (-2, +1), (-1, 0)]) / 4)
936929

937930
def _set_x_filled(self):
938931
self._transform = Affine2D()

lib/matplotlib/patches.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,11 +1269,9 @@ class Arrow(Patch):
12691269
def __str__(self):
12701270
return "Arrow()"
12711271

1272-
_path = Path([[0.0, 0.1], [0.0, -0.1],
1273-
[0.8, -0.1], [0.8, -0.3],
1274-
[1.0, 0.0], [0.8, 0.3],
1275-
[0.8, 0.1], [0.0, 0.1]],
1276-
closed=True)
1272+
_path = Path._create_closed([
1273+
[0.0, 0.1], [0.0, -0.1], [0.8, -0.1], [0.8, -0.3], [1.0, 0.0],
1274+
[0.8, 0.3], [0.8, 0.1]])
12771275

12781276
@_docstring.dedent_interpd
12791277
@_api.make_keyword_only("3.6", name="width")
@@ -2303,8 +2301,8 @@ def __call__(self, x0, y0, width, height, mutation_size):
23032301
# boundary of the padded box
23042302
x0, y0 = x0 - pad, y0 - pad
23052303
x1, y1 = x0 + width, y0 + height
2306-
return Path([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)],
2307-
closed=True)
2304+
return Path._create_closed(
2305+
[(x0, y0), (x1, y0), (x1, y1), (x0, y1)])
23082306

23092307
@_register_style(_style_list)
23102308
class Circle:
@@ -2353,11 +2351,11 @@ def __call__(self, x0, y0, width, height, mutation_size):
23532351
dxx = dx / 2
23542352
x0 = x0 + pad / 1.4 # adjust by ~sqrt(2)
23552353

2356-
return Path([(x0 + dxx, y0), (x1, y0), (x1, y1), (x0 + dxx, y1),
2357-
(x0 + dxx, y1 + dxx), (x0 - dx, y0 + dx),
2358-
(x0 + dxx, y0 - dxx), # arrow
2359-
(x0 + dxx, y0), (x0 + dxx, y0)],
2360-
closed=True)
2354+
return Path._create_closed(
2355+
[(x0 + dxx, y0), (x1, y0), (x1, y1), (x0 + dxx, y1),
2356+
(x0 + dxx, y1 + dxx), (x0 - dx, y0 + dx),
2357+
(x0 + dxx, y0 - dxx), # arrow
2358+
(x0 + dxx, y0)])
23612359

23622360
@_register_style(_style_list)
23632361
class RArrow(LArrow):
@@ -2397,14 +2395,14 @@ def __call__(self, x0, y0, width, height, mutation_size):
23972395
dxx = dx / 2
23982396
x0 = x0 + pad / 1.4 # adjust by ~sqrt(2)
23992397

2400-
return Path([(x0 + dxx, y0), (x1, y0), # bot-segment
2401-
(x1, y0 - dxx), (x1 + dx + dxx, y0 + dx),
2402-
(x1, y1 + dxx), # right-arrow
2403-
(x1, y1), (x0 + dxx, y1), # top-segment
2404-
(x0 + dxx, y1 + dxx), (x0 - dx, y0 + dx),
2405-
(x0 + dxx, y0 - dxx), # left-arrow
2406-
(x0 + dxx, y0), (x0 + dxx, y0)], # close-poly
2407-
closed=True)
2398+
return Path._create_closed([
2399+
(x0 + dxx, y0), (x1, y0), # bot-segment
2400+
(x1, y0 - dxx), (x1 + dx + dxx, y0 + dx),
2401+
(x1, y1 + dxx), # right-arrow
2402+
(x1, y1), (x0 + dxx, y1), # top-segment
2403+
(x0 + dxx, y1 + dxx), (x0 - dx, y0 + dx),
2404+
(x0 + dxx, y0 - dxx), # left-arrow
2405+
(x0 + dxx, y0)])
24082406

24092407
@_register_style(_style_list)
24102408
class Round:

lib/matplotlib/path.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ def _fast_from_codes_and_verts(cls, verts, codes, internals_from=None):
188188
pth._interpolation_steps = 1
189189
return pth
190190

191+
@classmethod
192+
def _create_closed(cls, vertices):
193+
"""
194+
Create a closed polygonal path going through *vertices*.
195+
196+
Unlike ``Path(..., closed=True)``, *vertices* should **not** end with
197+
an entry for the CLOSEPATH; this entry is added by `._create_closed`.
198+
"""
199+
v = _to_unmasked_float_array(vertices)
200+
return cls(np.concatenate([v, v[:1]]), closed=True)
201+
191202
def _update_values(self):
192203
self._simplify_threshold = mpl.rcParams['path.simplify_threshold']
193204
self._should_simplify = (

lib/matplotlib/tests/test_lines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def test_marker_as_markerstyle():
296296
line.set_marker(MarkerStyle("o"))
297297
fig.canvas.draw()
298298
# test Path roundtrip
299-
triangle1 = Path([[-1., -1.], [1., -1.], [0., 2.], [0., 0.]], closed=True)
299+
triangle1 = Path._create_closed([[-1, -1], [1, -1], [0, 2]])
300300
line2, = ax.plot([1, 3, 2], marker=MarkerStyle(triangle1), ms=22)
301301
line3, = ax.plot([0, 2, 1], marker=triangle1, ms=22)
302302

lib/matplotlib/tests/test_path.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ def test_path_exceptions():
5353

5454
def test_point_in_path():
5555
# Test #1787
56-
verts2 = [(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]
57-
58-
path = Path(verts2, closed=True)
56+
path = Path._create_closed([(0, 0), (0, 1), (1, 1), (1, 0)])
5957
points = [(0.5, 0.5), (1.5, 0.5)]
6058
ret = path.contains_points(points)
6159
assert ret.dtype == 'bool'

lib/matplotlib/tests/test_transforms.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ def test_affine_inverted_invalidated():
191191

192192
def test_clipping_of_log():
193193
# issue 804
194-
path = Path([(0.2, -99), (0.4, -99), (0.4, 20), (0.2, 20), (0.2, -99)],
195-
closed=True)
194+
path = Path._create_closed([(0.2, -99), (0.4, -99), (0.4, 20), (0.2, 20)])
196195
# something like this happens in plotting logarithmic histograms
197196
trans = mtransforms.BlendedGenericTransform(
198197
mtransforms.Affine2D(), scale.LogTransform(10, 'clip'))

lib/mpl_toolkits/axes_grid1/inset_locator.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ def __init__(self, bbox, **kwargs):
160160
def get_path(self):
161161
# docstring inherited
162162
x0, y0, x1, y1 = self.bbox.extents
163-
return Path([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)],
164-
closed=True)
163+
return Path._create_closed([(x0, y0), (x1, y0), (x1, y1), (x0, y1)])
165164

166165

167166
class BboxConnector(Patch):

0 commit comments

Comments
 (0)