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

Skip to content

Commit c6b11f0

Browse files
authored
Merge pull request #18030 from QuLogic/fix-poly-verts-opt
Fix PolyCollection.set_verts optimization.
2 parents 4c43774 + d10c4c4 commit c6b11f0

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/matplotlib/collections.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ def __init__(self, verts, sizes=None, closed=True, **kwargs):
11091109
verts : list of array-like
11101110
The sequence of polygons [*verts0*, *verts1*, ...] where each
11111111
element *verts_i* defines the vertices of polygon *i* as a 2D
1112-
array-like of of shape (M, 2).
1112+
array-like of shape (M, 2).
11131113
sizes : array-like, default: None
11141114
Squared scaling factors for the polygons. The coordinates of each
11151115
polygon *verts_i* are multiplied by the square-root of the
@@ -1136,7 +1136,7 @@ def set_verts(self, verts, closed=True):
11361136
verts : list of array-like
11371137
The sequence of polygons [*verts0*, *verts1*, ...] where each
11381138
element *verts_i* defines the vertices of polygon *i* as a 2D
1139-
array-like of of shape (M, 2).
1139+
array-like of shape (M, 2).
11401140
closed : bool, default: True
11411141
Whether the polygon should be closed by adding a CLOSEPOLY
11421142
connection at the end.
@@ -1151,7 +1151,7 @@ def set_verts(self, verts, closed=True):
11511151
return
11521152

11531153
# Fast path for arrays
1154-
if isinstance(verts, np.ndarray):
1154+
if isinstance(verts, np.ndarray) and len(verts.shape) == 3:
11551155
verts_pad = np.concatenate((verts, verts[:, :1]), axis=1)
11561156
# Creating the codes once is much faster than having Path do it
11571157
# separately each time by passing closed=True.

lib/matplotlib/tests/test_collections.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,14 @@ def test_collection_set_verts_array():
615615
assert np.array_equal(ap._vertices, lp._vertices)
616616
assert np.array_equal(ap._codes, lp._codes)
617617

618+
verts_tuple = np.empty(10, dtype=object)
619+
verts_tuple[:] = [tuple(tuple(y) for y in x) for x in verts]
620+
col_arr_tuple = PolyCollection(verts_tuple)
621+
assert len(col_arr._paths) == len(col_arr_tuple._paths)
622+
for ap, atp in zip(col_arr._paths, col_arr_tuple._paths):
623+
assert np.array_equal(ap._vertices, atp._vertices)
624+
assert np.array_equal(ap._codes, atp._codes)
625+
618626

619627
def test_blended_collection_autolim():
620628
a = [1, 2, 4]

0 commit comments

Comments
 (0)