8000 Backport PR #18030: Fix PolyCollection.set_verts optimization. · matplotlib/matplotlib@ba9e2da · GitHub
[go: up one dir, main page]

Skip to content

Commit ba9e2da

Browse files
jklymakmeeseeksmachine
authored andcommitted
Backport PR #18030: Fix PolyCollection.set_verts optimization.
1 parent fabcc80 commit ba9e2da

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
@@ -1107,7 +1107,7 @@ def __init__(self, verts, sizes=None, closed=True, **kwargs):
11071107
verts : list of array-like
11081108
The sequence of polygons [*verts0*, *verts1*, ...] where each
11091109
element *verts_i* defines the vertices of polygon *i* as a 2D
1110-
array-like of of shape (M, 2).
1110+
array-like of shape (M, 2).
11111111
sizes : array-like, default: None
11121112
Squared scaling factors for the polygons. The coordinates of each
11131113
polygon *verts_i* are multiplied by the square-root of the
@@ -1134,7 +1134,7 @@ def set_verts(self, verts, closed=True):
11341134
verts : list of array-like
11351135
The sequence of polygons [*verts0*, *verts1*, ...] where each
11361136
element *verts_i* defines the vertices of polygon *i* as a 2D
1137-
array-like of of shape (M, 2).
1137+
array-like of shape (M, 2).
11381138
closed : bool, default: True
11391139
Whether the polygon should be closed by adding a CLOSEPOLY
11401140
connection at the end.
@@ -1149,7 +1149,7 @@ def set_verts(self, verts, closed=True):
11491149
return
11501150

11511151
# Fast path for arrays
1152-
if isinstance(verts, np.ndarray):
1152+
if isinstance(verts, np.ndarray) and len(verts.shape) == 3:
11531153
verts_pad = np.concatenate((verts, verts[:, :1]), axis=1)
11541154
# Creating the codes once is much faster than having Path do it
11551155
# 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)
0