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

Skip to content

Add a fast path for NumPy arrays to Collection.set_verts #16689

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 1 commit into from
Mar 16, 2020
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
42 changes: 28 additions & 14 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,23 +1090,37 @@ def set_verts(self, verts, closed=True):
Whether the polygon should be closed by adding a CLOSEPOLY
connection at the end.
"""
self.stale = True
if isinstance(verts, np.ma.MaskedArray):
verts = verts.astype(float).filled(np.nan)
# This is much faster than having Path do it one at a time.
if closed:
self._paths = []
for xy in verts:
if len(xy):
if isinstance(xy, np.ma.MaskedArray):
xy = np.ma.concatenate([xy, xy[:1]])
else:
xy = np.concatenate([xy, xy[:1]])
self._paths.append(mpath.Path(xy, closed=True))
else:
self._paths.append(mpath.Path(xy))
else:

# No need to do anything fancy if the path isn't closed.
if not closed:
self._paths = [mpath.Path(xy) for xy in verts]
self.stale = True
return

# Fast path for arrays
if isinstance(verts, np.ndarray):
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]
return

self._paths = []
for xy in verts:
if len(xy):
if isinstance(xy, np.ma.MaskedArray):
xy = np.ma.concatenate([xy, xy[:1]])
else:
xy = np.concatenate([xy, xy[:1]])
self._paths.append(mpath.Path(xy, closed=True))
else:
self._paths.append(mpath.Path(xy))

set_paths = set_verts

Expand Down
13 changes: 12 additions & 1 deletion lib/matplotlib/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import matplotlib.pyplot as plt
import matplotlib.collections as mcollections
import matplotlib.transforms as mtransforms
from matplotlib.collections import Collection, LineCollection, EventCollection
from matplotlib.collections import (Collection, LineCollection,
EventCollection, PolyCollection)
from matplotlib.testing.decorators import image_comparison


Expand Down Expand Up @@ -612,3 +613,13 @@ def test_EventCollection_nosort():
arr = np.array([3, 2, 1, 10])
coll = EventCollection(arr)
np.testing.assert_array_equal(arr, np.array([3, 2, 1, 10]))


def test_collection_set_verts_array():
verts = np.arange(80, dtype=np.double).reshape(10, 4, 2)
col_arr = PolyCollection(verts)
col_list = PolyCollection(list(verts))
assert len(col_arr._paths) == len(col_list._paths)
for ap, lp in zip(col_arr._paths, col_list._paths):
assert np.array_equal(ap._vertices, lp._vertices)
assert np.array_equal(ap._codes, lp._codes)