Vectorize patch extraction in Axes3D.plot_surface by apaszke · Pull Request #16675 · matplotlib/matplotlib · GitHub
Skip to content
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
101 changes: 101 additions & 0 deletions lib/matplotlib/cbook/__init__.py
28 changes: 28 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,31 @@ def test_warn_external(recwarn):
cbook._warn_external("oops")
assert len(recwarn) == 1
assert recwarn[0].filename == __file__


def test_array_patch_perimeters():
# This compares the old implementation as a reference for the
# vectorized one.
def check(x, rstride, cstride):
Comment thread
apaszke marked this conversation as resolved.
rows, cols = x.shape
row_inds = [*range(0, rows-1, rstride), rows-1]
col_inds = [*range(0, cols-1, cstride), cols-1]
polys = []
for rs, rs_next in zip(row_inds[:-1], row_inds[1:]):
for cs, cs_next in zip(col_inds[:-1], col_inds[1:]):
# +1 ensures we share edges between polygons
ps = cbook._array_perimeter(x[rs:rs_next+1, cs:cs_next+1]).T
polys.append(ps)
polys = np.asarray(polys)
assert np.array_equal(polys,
cbook._array_patch_perimeters(
x, rstride=rstride, cstride=cstride))

def divisors(n):
return [i for i in range(1, n + 1) if n % i == 0]

for rows, cols in [(5, 5), (7, 14), (13, 9)]:
x = np.arange(rows * cols).reshape(rows, cols)
for rstride, cstride in itertools.product(divisors(rows - 1),
divisors(cols - 1)):
check(x, rstride=rstride, cstride=cstride)
62 changes: 42 additions & 20 deletions lib/mpl_toolkits/mplot3d/axes3d.py