Skip to content

Commit acf4ac4

Browse files
committed
ENH: optimize Collection non-affine transform to only call transform once
1 parent 341070a commit acf4ac4

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

lib/matplotlib/collections.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def get_datalim(self, transData):
189189
paths = self.get_paths()
190190

191191
if not transform.is_affine:
192-
paths = [transform.transform_path_non_affine(p) for p in paths]
192+
paths = self._non_affine_transform_paths(paths, transform)
193193
transform = transform.get_affine()
194194
if not transOffset.is_affine:
195195
offsets = transOffset.transform_non_affine(offsets)
@@ -213,6 +213,22 @@ def get_window_extent(self, renderer):
213213
# cases other than scatter plot legend
214214
return self.get_datalim(transforms.IdentityTransform())
215215

216+
def _non_affine_transform_paths(self, paths, transform):
217+
"""
218+
Sometimes there will be many paths, and the transform is
219+
slow to run. This helper concatenates the paths, transforms,
220+
the points, and then splits them back into the individual paths.
221+
"""
222+
vertices = np.vstack(path.vertices for path in paths)
223+
pos = np.cumsum([len(x) for x in paths])
224+
vertices = transform.transform_path_non_affine(vertices)
225+
# repopulate paths
226+
verts = np.split(vertices, pos[:-1])
227+
for nn, verti in enumerate(verts):
228+
paths[nn].vertices = verti
229+
230+
return paths
231+
216232
def _prepare_points(self):
217233
"""Point prep for drawing and hit testing"""
218234

@@ -236,8 +252,7 @@ def _prepare_points(self):
236252
offsets = np.column_stack([xs, ys])
237253

238254
if not transform.is_affine:
239-
paths = [transform.transform_path_non_affine(path)
240-
for path in paths]
255+
paths = self._non_affine_transform_paths(paths, transform)
241256
transform = transform.get_affine()
242257
if not transOffset.is_affine:
243258
offsets = transOffset.transform_non_affine(offsets)

0 commit comments

Comments
 (0)