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

Skip to content

Commit ad9ba1e

Browse files
authored
Merge pull request #16617 from anntzer/closed
Use Path(..., closed=True) more.
2 parents 7d4de7c + 7c9c01b commit ad9ba1e

File tree

9 files changed

+79
-190
lines changed

9 files changed

+79
-190
lines changed

examples/animation/animated_histogram.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
# in the ``verts`` array to keep the codes aligned with the vertices.
4646
nverts = nrects * (1 + 3 + 1)
4747
verts = np.zeros((nverts, 2))
48-
codes = np.ones(nverts, int) * path.Path.LINETO
48+
codes = np.full(nverts, path.Path.LINETO)
4949
codes[0::5] = path.Path.MOVETO
5050
codes[4::5] = path.Path.CLOSEPOLY
5151
verts[0::5, 0] = left

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3964,17 +3964,11 @@ def line_props_with_rcdefaults(subkey, explicit, zdelta=0,
39643964
if meanprops is None or removed_prop not in meanprops:
39653965
final_meanprops[removed_prop] = ''
39663966

3967-
def to_vc(xs, ys):
3968-
# convert arguments to verts and codes, append (0, 0) (ignored).
3969-
verts = np.append(np.column_stack([xs, ys]), [(0, 0)], 0)
3970-
codes = ([mpath.Path.MOVETO]
3971-
+ [mpath.Path.LINETO] * (len(verts) - 2)
3972-
+ [mpath.Path.CLOSEPOLY])
3973-
return verts, codes
3974-
39753967
def patch_list(xs, ys, **kwargs):
3976-
verts, codes = to_vc(xs, ys)
3977-
path = mpath.Path(verts, codes)
3968+
path = mpath.Path(
3969+
# Last vertex will have a CLOSEPOLY code and thus be ignored.
3970+
np.append(np.column_stack([xs, ys]), [(0, 0)], 0),
3971+
closed=True)
39783972
patch = mpatches.PathPatch(path, **kwargs)
39793973
self.add_artist(patch)
39803974
return [patch]

lib/matplotlib/collections.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,15 +1098,10 @@ def set_verts(self, verts, closed=True):
10981098
for xy in verts:
10991099
if len(xy):
11001100
if isinstance(xy, np.ma.MaskedArray):
1101-
xy = np.ma.concatenate([xy, xy[0:1]])
1101+
xy = np.ma.concatenate([xy, xy[:1]])
11021102
else:
1103-
xy = np.asarray(xy)
1104-
xy = np.concatenate([xy, xy[0:1]])
1105-
codes = np.empty(xy.shape[0], dtype=mpath.Path.code_type)
1106-
codes[:] = mpath.Path.LINETO
1107-
codes[0] = mpath.Path.MOVETO
1108-
codes[-1] = mpath.Path.CLOSEPOLY
1109-
self._paths.append(mpath.Path(xy, codes))
1103+
xy = np.concatenate([xy, xy[:1]])
1104+
self._paths.append(mpath.Path(xy, closed=True))
11101105
else:
11111106
self._paths.append(mpath.Path(xy))
11121107
else:

lib/matplotlib/markers.py

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -410,23 +410,15 @@ def _set_pixel(self):
410410
def _set_point(self):
411411
self._set_circle(reduction=self._point_size_reduction)
412412

413-
_triangle_path = Path(
414-
[[0.0, 1.0], [-1.0, -1.0], [1.0, -1.0], [0.0, 1.0]],
415-
[Path.MOVETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY])
413+
_triangle_path = Path([[0, 1], [-1, -1], [1, -1], [0, 1]], closed=True)
416414
# Going down halfway looks to small. Golden ratio is too far.
417-
_triangle_path_u = Path(
418-
[[0.0, 1.0], [-3 / 5., -1 / 5.], [3 / 5., -1 / 5.], [0.0, 1.0]],
419-
[Path.MOVETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY])
415+
_triangle_path_u = Path([[0, 1], [-3/5, -1/5], [3/5, -1/5], [0, 1]],
416+
closed=True)
420417
_triangle_path_d = Path(
421-
[[-3 / 5., -1 / 5.], [3 / 5., -1 / 5.], [1.0, -1.0], [-1.0, -1.0],
422-
[-3 / 5., -1 / 5.]],
423-
[Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY])
424-
_triangle_path_l = Path(
425-
[[0.0, 1.0], [0.0, -1.0], [-1.0, -1.0], [0.0, 1.0]],
426-
[Path.MOVETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY])
427-
_triangle_path_r = Path(
428-
[[0.0, 1.0], [0.0, -1.0], [1.0, -1.0], [0.0, 1.0]],
429-
[Path.MOVETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY])
418+
[[-3/5, -1/5], [3/5, -1/5], [1, -1], [-1, -1], [-3/5, -1/5]],
419+
closed=True)
420+
_triangle_path_l = Path([[0, 1], [0, -1], [-1, -1], [0, 1]], closed=True)
421+
_triangle_path_r = Path([[0, 1], [0, -1], [1, -1], [0, 1]], closed=True)
430422

431423
def _set_triangle(self, rot, skip):
432424
self._transform = Affine2D().scale(0.5).rotate_deg(rot)
@@ -505,10 +497,8 @@ def _set_diamond(self):
505497
if not self._half_fill():
506498
self._path = Path.unit_rectangle()
507499
else:
508-
self._path = Path([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 0.0]])
509-
self._alt_path = Path([[0.0, 0.0], [0.0, 1.0],
510-
[1.0, 1.0], [0.0, 0.0]])
511-
500+
self._path = Path([[0, 0], [1, 0], [1, 1], [0, 0]])
501+
self._alt_path = Path([[0, 0], [0, 1], [1, 1], [0, 0]])
512502
if fs == 'bottom':
513503
rotate = 270.
514504
elif fs == 'top':
@@ -517,10 +507,8 @@ def _set_diamond(self):
517507
rotate = 180.
518508
else:
519509
rotate = 0.
520-
521510
self._transform.rotate_deg(rotate)
522511
self._alt_transform = self._transform
523-
524512
self._joinstyle = 'miter'
525513

526514
def _set_thin_diamond(self):
@@ -815,24 +803,13 @@ def _set_x(self):
815803
self._filled = False
816804
self._path = self._x_path
817805

818-
_plus_filled_path = Path([(1/3, 0), (2/3, 0), (2/3, 1/3),
819-
(1, 1/3), (1, 2/3), (2/3, 2/3),
820-
(2/3, 1), (1/3, 1), (1/3, 2/3),
821-
(0, 2/3), (0, 1/3), (1/3, 1/3),
822-
(1/3, 0)],
823-
[Path.MOVETO, Path.LINETO, Path.LINETO,
824-
Path.LINETO, Path.LINETO, Path.LINETO,
825-
Path.LINETO, Path.LINETO, Path.LINETO,
826-
Path.LINETO, Path.LINETO, Path.LINETO,
827-
Path.CLOSEPOLY])
828-
829-
_plus_filled_path_t = Path([(1, 1/2), (1, 2/3), (2/3, 2/3),
830-
(2/3, 1), (1/3, 1), (1/3, 2/3),
831-
(0, 2/3), (0, 1/2), (1, 1/2)],
832-
[Path.MOVETO, Path.LINETO, Path.LINETO,
833-
Path.LINETO, Path.LINETO, Path.LINETO,
834-
Path.LINETO, Path.LINETO,
835-
Path.CLOSEPOLY])
806+
_plus_filled_path = Path(
807+
[(1/3, 0), (2/3, 0), (2/3, 1/3), (1, 1/3), (1, 2/3), (2/3, 2/3),
808+
(2/3, 1), (1/3, 1), (1/3, 2/3), (0, 2/3), (0, 1/3), (1/3, 1/3),
809+
(1/3, 0)], closed=True)
810+
_plus_filled_path_t = Path(
811+
[(1, 1/2), (1, 2/3), (2/3, 2/3), (2/3, 1), (1/3, 1), (1/3, 2/3),
812+
(0, 2/3), (0, 1/2), (1, 1/2)], closed=True)
836813

837814
def _set_plus_filled(self):
838815
self._transform = Affine2D().translate(-0.5, -0.5)
@@ -858,22 +835,13 @@ def _set_plus_filled(self):
858835
self._transform.rotate_deg(rotate)
859836
self._alt_transform.rotate_deg(rotate_alt)
860837

861-
_x_filled_path = Path([(0.25, 0), (0.5, 0.25), (0.75, 0), (1, 0.25),
862-
(0.75, 0.5), (1, 0.75), (0.75, 1), (0.5, 0.75),
863-
(0.25, 1), (0, 0.75), (0.25, 0.5), (0, 0.25),
864-
(0.25, 0)],
865-
[Path.MOVETO, Path.LINETO, Path.LINETO,
866-
Path.LINETO, Path.LINETO, Path.LINETO,
867-
Path.LINETO, Path.LINETO, Path.LINETO,
868-
Path.LINETO, Path.LINETO, Path.LINETO,
869-
Path.CLOSEPOLY])
870-
871-
_x_filled_path_t = Path([(0.75, 0.5), (1, 0.75), (0.75, 1),
872-
(0.5, 0.75), (0.25, 1), (0, 0.75),
873-
(0.25, 0.5), (0.75, 0.5)],
874-
[Path.MOVETO, Path.LINETO, Path.LINETO,
875-
Path.LINETO, Path.LINETO, Path.LINETO,
876-
Path.LINETO, Path.CLOSEPOLY])
838+
_x_filled_path = Path(
839+
[(0.25, 0), (0.5, 0.25), (0.75, 0), (1, 0.25), (0.75, 0.5), (1, 0.75),
840+
(0.75, 1), (0.5, 0.75), (0.25, 1), (0, 0.75), (0.25, 0.5), (0, 0.25),
841+
(0.25, 0)], closed=True)
842+
_x_filled_path_t = Path(
843+
[(0.75, 0.5), (1, 0.75), (0.75, 1), (0.5, 0.75), (0.25, 1), (0, 0.75),
844+
(0.25, 0.5), (0.75, 0.5)], closed=True)
877845

878846
def _set_x_filled(self):
879847
self._transform = Affine2D().translate(-0.5, -0.5)

lib/matplotlib/patches.py

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,17 +1983,13 @@ def __init__(self, pad=0.3):
19831983

19841984
def transmute(self, x0, y0, width, height, mutation_size):
19851985
pad = mutation_size * self.pad
1986-
19871986
# width and height with padding added.
1988-
width, height = width + 2*pad, height + 2*pad
1989-
1987+
width, height = width + 2 * pad, height + 2 * pad
19901988
# boundary of the padded box
1991-
x0, y0 = x0 - pad, y0 - pad,
1989+
x0, y0 = x0 - pad, y0 - pad
19921990
x1, y1 = x0 + width, y0 + height
1993-
1994-
vertices = [(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)]
1995-
codes = [Path.MOVETO] + [Path.LINETO] * 3 + [Path.CLOSEPOLY]
1996-
return Path(vertices, codes)
1991+
return Path([(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)],
1992+
closed=True)
19971993

19981994
@_register_style(_style_list)
19991995
class Circle(_Base):
@@ -2012,9 +2008,8 @@ def __init__(self, pad=0.3):
20122008
def transmute(self, x0, y0, width, height, mutation_size):
20132009
pad = mutation_size * self.pad
20142010
width, height = width + 2 * pad, height + 2 * pad
2015-
20162011
# boundary of the padded box
2017-
x0, y0 = x0 - pad, y0 - pad,
2012+
x0, y0 = x0 - pad, y0 - pad
20182013
return Path.circle((x0 + width / 2, y0 + height / 2),
20192014
max(width, height) / 2)
20202015

@@ -2035,31 +2030,21 @@ def __init__(self, pad=0.3):
20352030
def transmute(self, x0, y0, width, height, mutation_size):
20362031
# padding
20372032
pad = mutation_size * self.pad
2038-
20392033
# width and height with padding added.
2040-
width, height = width + 2. * pad, height + 2. * pad
2041-
2034+
width, height = width + 2 * pad, height + 2 * pad
20422035
# boundary of the padded box
20432036
x0, y0 = x0 - pad, y0 - pad,
20442037
x1, y1 = x0 + width, y0 + height
20452038

2046-
dx = (y1 - y0) / 2.
2047-
dxx = dx * .5
2048-
# adjust x0. 1.4 <- sqrt(2)
2049-
x0 = x0 + pad / 1.4
2050-
2051-
cp = [(x0 + dxx, y0), (x1, y0), (x1, y1), (x0 + dxx, y1),
2052-
(x0 + dxx, y1 + dxx), (x0 - dx, y0 + dx),
2053-
(x0 + dxx, y0 - dxx), # arrow
2054-
(x0 + dxx, y0), (x0 + dxx, y0)]
2055-
2056-
com = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO,
2057-
Path.LINETO, Path.LINETO, Path.LINETO,
2058-
Path.LINETO, Path.CLOSEPOLY]
2059-
2060-
path = Path(cp, com)
2039+
dx = (y1 - y0) / 2
2040+
dxx = dx / 2
2041+
x0 = x0 + pad / 1.4 # adjust by ~sqrt(2)
20612042

2062-
return path
2043+
return Path([(x0 + dxx, y0), (x1, y0), (x1, y1), (x0 + dxx, y1),
2044+
(x0 + dxx, y1 + dxx), (x0 - dx, y0 + dx),
2045+
(x0 + dxx, y0 - dxx), # arrow
2046+
(x0 + dxx, y0), (x0 + dxx, y0)],
2047+
closed=True)
20632048

20642049
@_register_style(_style_list)
20652050
class RArrow(LArrow):
@@ -2098,42 +2083,27 @@ def __init__(self, pad=0.3):
20982083
super().__init__()
20992084

21002085
def transmute(self, x0, y0, width, height, mutation_size):
2101-
21022086
# padding
21032087
pad = mutation_size * self.pad
2104-
21052088
# width and height with padding added.
21062089
# The width is padded by the arrows, so we don't need to pad it.
2107-
height = height + 2. * pad
2108-
2090+
height = height + 2 * pad
21092091
# boundary of the padded box
21102092
x0, y0 = x0 - pad, y0 - pad
21112093
x1, y1 = x0 + width, y0 + height
21122094

21132095
dx = (y1 - y0) / 2
2114-
dxx = dx * .5
2115-
# adjust x0. 1.4 <- sqrt(2)
2116-
x0 = x0 + pad / 1.4
2117-
2118-
cp = [(x0 + dxx, y0), (x1, y0), # bot-segment
2119-
(x1, y0 - dxx), (x1 + dx + dxx, y0 + dx),
2120-
(x1, y1 + dxx), # right-arrow
2121-
(x1, y1), (x0 + dxx, y1), # top-segment
2122-
(x0 + dxx, y1 + dxx), (x0 - dx, y0 + dx),
2123-
(x0 + dxx, y0 - dxx), # left-arrow
2124-
(x0 + dxx, y0), (x0 + dxx, y0)] # close-poly
2125-
2126-
com = [Path.MOVETO, Path.LINETO,
2127-
Path.LINETO, Path.LINETO,
2128-
Path.LINETO,
2129-
Path.LINETO, Path.LINETO,
2130-
Path.LINETO, Path.LINETO,
2131-
Path.LINETO,
2132-
Path.LINETO, Path.CLOSEPOLY]
2133-
2134-
path = Path(cp, com)
2135-
2136-
return path
2096+
dxx = dx / 2
2097+
x0 = x0 + pad / 1.4 # adjust by ~sqrt(2)
2098+
2099+
return Path([(x0 + dxx, y0), (x1, y0), # bot-segment
2100+
(x1, y0 - dxx), (x1 + dx + dxx, y0 + dx),
2101+
(x1, y1 + dxx), # right-arrow
2102+
(x1, y1), (x0 + dxx, y1), # top-segment
2103+
(x0 + dxx, y1 + dxx), (x0 - dx, y0 + dx),
2104+
(x0 + dxx, y0 - dxx), # left-arrow
2105+
(x0 + dxx, y0), (x0 + dxx, y0)], # close-poly
2106+
closed=True)
21372107

21382108
@_register_style(_style_list)
21392109
class Round(_Base):
@@ -2163,7 +2133,7 @@ def transmute(self, x0, y0, width, height, mutation_size):
21632133
else:
21642134
dr = pad
21652135

2166-
width, height = width + 2. * pad, height + 2. * pad
2136+
width, height = width + 2 * pad, height + 2 * pad
21672137

21682138
x0, y0 = x0 - pad, y0 - pad,
21692139
x1, y1 = x0 + width, y0 + height
@@ -2224,8 +2194,8 @@ def transmute(self, x0, y0, width, height, mutation_size):
22242194
else:
22252195
dr = pad / 2.
22262196

2227-
width, height = (width + 2. * pad - 2 * dr,
2228-
height + 2. * pad - 2 * dr)
2197+
width = width + 2 * pad - 2 * dr
2198+
height = height + 2 * pad - 2 * dr
22292199

22302200
x0, y0 = x0 - pad + dr, y0 - pad + dr,
22312201
x1, y1 = x0 + width, y0 + height

lib/matplotlib/path.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ def __init__(self, vertices, codes=None, _interpolation_steps=1,
120120
intended for public use.
121121
closed : bool, optional
122122
If *codes* is None and closed is True, vertices will be treated as
123-
line segments of a closed polygon.
123+
line segments of a closed polygon. Note that the last vertex will
124+
then be ignored (as the corresponding code will be set to
125+
CLOSEPOLY).
124126
readonly : bool, optional
125127
Makes the path behave in an immutable way and sets the vertices
126128
and codes as read-only arrays.
@@ -635,12 +637,8 @@ def unit_rectangle(cls):
635637
Return a `Path` instance of the unit rectangle from (0, 0) to (1, 1).
636638
"""
637639
if cls._unit_rectangle is None:
638-
cls._unit_rectangle = \
639-
cls([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0],
640-
[0.0, 0.0]],
641-
[cls.MOVETO, cls.LINETO, cls.LINETO, cls.LINETO,
642-
cls.CLOSEPOLY],
643-
readonly=True)
640+
cls._unit_rectangle = cls([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]],
641+
closed=True, readonly=True)
644642
return cls._unit_rectangle
645643

646644
_unit_regular_polygons = WeakValueDictionary()
@@ -661,11 +659,7 @@ def unit_regular_polygon(cls, numVertices):
661659
# "points-up".
662660
+ np.pi / 2)
663661
verts = np.column_stack((np.cos(theta), np.sin(theta)))
664-
codes = np.empty(numVertices + 1)
665-
codes[0] = cls.MOVETO
666-
codes[1:-1] = cls.LINETO
667-
codes[-1] = cls.CLOSEPOLY
668-
path = cls(verts, codes, readonly=True)
662+
path = cls(verts, closed=True, readonly=True)
669663
if numVertices <= 16:
670664
cls._unit_regular_polygons[numVertices] = path
671665
return path
@@ -691,11 +685,7 @@ def unit_regular_star(cls, numVertices, innerCircle=0.5):
691685
r = np.ones(ns2 + 1)
692686
r[1::2] = innerCircle
693687
verts = (r * np.vstack((np.cos(theta), np.sin(theta)))).T
694-
codes = np.empty(ns2 + 1)
695-
codes[0] = cls.MOVETO
696-
codes[1:-1] = cls.LINETO
697-
codes[-1] = cls.CLOSEPOLY
698-
path = cls(verts, codes, readonly=True)
688+
path = cls(verts, closed=True, readonly=True)
699689
if numVertices <= 16:
700690
cls._unit_regular_stars[(numVertices, innerCircle)] = path
701691
return path

lib/matplotlib/tests/test_patches.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,15 @@ def test_patch_str():
319319
assert str(p) == "FancyBboxPatch((1, 2), width=3, height=4)"
320320

321321
# Further nice __str__ which cannot be `eval`uated:
322-
path_data = [([1, 2], mpath.Path.MOVETO), ([2, 2], mpath.Path.LINETO),
323-
([1, 2], mpath.Path.CLOSEPOLY)]
324-
p = mpatches.PathPatch(mpath.Path(*zip(*path_data)))
322+
path = mpath.Path([(1, 2), (2, 2), (1, 2)], closed=True)
323+
p = mpatches.PathPatch(path)
325324
assert str(p) == "PathPatch3((1, 2) ...)"
326325

327326
data = [[1, 2], [2, 2], [1, 2]]
328327
p = mpatches.Polygon(data)
329328
assert str(p) == "Polygon3((1, 2) ...)"
330329

331-
p = mpatches.FancyArrowPatch(path=mpath.Path(*zip(*path_data)))
330+
p = mpatches.FancyArrowPatch(path=path)
332331
assert str(p)[:27] == "FancyArrowPatch(Path(array("
333332

334333
p = mpatches.FancyArrowPatch((1, 2), (3, 4))

0 commit comments

Comments
 (0)