Skip to content

Speed up Collection.set_paths #29398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -1340,9 +1340,9 @@ def _to_unmasked_float_array(x):
values are converted to nans.
"""
if hasattr(x, 'mask'):
return np.ma.asarray(x, float).filled(np.nan)
return np.ma.asanyarray(x, float).filled(np.nan)
else:
return np.asarray(x, float)
return np.asanyarray(x, float)


def _check_1d(x):
Expand Down
16 changes: 8 additions & 8 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,15 +1242,15 @@ def set_verts(self, verts, closed=True):
return

# Fast path for arrays
if isinstance(verts, np.ndarray) and len(verts.shape) == 3:
if isinstance(verts, np.ndarray) and len(verts.shape) == 3 and verts.size:
verts_pad = np.concatenate((verts, verts[:, :1]), axis=1)
# Creating the codes once is much faster than having Path do it
# separately each time by passing closed=True.
codes = np.empty(verts_pad.shape[1], dtype=mpath.Path.code_type)
codes[:] = mpath.Path.LINETO
codes[0] = mpath.Path.MOVETO
codes[-1] = mpath.Path.CLOSEPOLY
self._paths = [mpath.Path(xy, codes) for xy in verts_pad]
# It's faster to create the codes and internal flags once in a
# template path and reuse them.
template_path = mpath.Path(verts_pad[0], closed=True)
codes = template_path.codes
_make_path = mpath.Path._fast_from_codes_and_verts
self._paths = [_make_path(xy, codes, internals_from=template_path)
for xy in verts_pad]
return

self._paths = []
Expand Down
1 change: 0 additions & 1 deletion lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ class Path:
made up front in the constructor that will not change when the
data changes.
"""

code_type = np.uint8

# Path codes
Expand Down
6 changes: 0 additions & 6 deletions lib/matplotlib/tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,12 +987,6 @@ def test_transformed_path():
[(0, 0), (r2, r2), (0, 2 * r2), (-r2, r2)],
atol=1e-15)

# Changing the path does not change the result (it's cached).
path.points = [(0, 0)] * 4
assert_allclose(trans_path.get_fully_transformed_path().vertices,
[(0, 0), (r2, r2), (0, 2 * r2), (-r2, r2)],
atol=1e-15)


def test_transformed_patch_path():
trans = mtransforms.Affine2D()
Expand Down
2 changes: 1 addition & 1 deletion lib/mpl_toolkits/mplot3d/art3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ def _zalpha(colors, zs):
# Should really normalize against the viewing depth.
if len(colors) == 0 or len(zs) == 0:
return np.zeros((0, 4))
norm = Normalize(min(zs), max(zs))
norm = Normalize(np.min(zs), np.max(zs))
sats = 1 - norm(zs) * 0.7
rgba = np.broadcast_to(mcolors.to_rgba_array(colors), (len(zs), 4))
return np.column_stack([rgba[:, :3], rgba[:, 3] * sats])
Expand Down
3 changes: 1 addition & 2 deletions lib/mpl_toolkits/mplot3d/proj3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ def _proj_transform_vectors(vecs, M):

def _proj_transform_vec_clip(vec, M, focal_length):
vecw = np.dot(M, vec.data)
w = vecw[3]
txs, tys, tzs = vecw[0] / w, vecw[1] / w, vecw[2] / w
txs, tys, tzs = vecw[0:3] / vecw[3]
if np.isinf(focal_length): # don't clip orthographic projection
tis = np.ones(txs.shape, dtype=bool)
else:
Expand Down