Skip to content

Commit 85ce7c5

Browse files
committed
Pad plot_surface inputs to allow for vectorized processing
1 parent 74ad0c2 commit 85ce7c5

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

+26-28
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ def plot_surface(self, X, Y, Z, *args, norm=None, vmin=None,
15401540
cstride = int(max(np.ceil(cols / ccount), 1))
15411541

15421542
if 'facecolors' in kwargs:
1543-
fcolors = kwargs.pop('facecolors')
1543+
fcolors = np.asarray(kwargs.pop('facecolors'))
15441544
else:
15451545
color = kwargs.pop('color', None)
15461546
if color is None:
@@ -1558,33 +1558,31 @@ def plot_surface(self, X, Y, Z, *args, norm=None, vmin=None,
15581558
"semantic or raise an error in matplotlib 3.3. "
15591559
"Please use shade=False instead.")
15601560

1561-
colset = [] # the sampled facecolor
1562-
if (rows - 1) % rstride == 0 and \
1563-
(cols - 1) % cstride == 0 and \
1564-
fcolors is None:
1565-
polys = np.stack(
1566-
[cbook._array_patch_perimeters(a, rstride, cstride)
1567-
for a in (X, Y, Z)],
1568-
axis=-1)
1569-
else:
1570-
# evenly spaced, and including both endpoints
1571-
row_inds = list(range(0, rows-1, rstride)) + [rows-1]
1572-
col_inds = list(range(0, cols-1, cstride)) + [cols-1]
1573-
1574-
polys = []
1575-
for rs, rs_next in zip(row_inds[:-1], row_inds[1:]):
1576-
for cs, cs_next in zip(col_inds[:-1], col_inds[1:]):
1577-
ps = [
1578-
# +1 ensures we share edges between polygons
1579-
cbook._array_perimeter(a[rs:rs_next+1, cs:cs_next+1])
1580-
for a in (X, Y, Z)
1581-
]
1582-
# ps = np.stack(ps, axis=-1)
1583-
ps = np.array(ps).T
1584-
polys.append(ps)
1585-
1586-
if fcolors is not None:
1587-
colset.append(fcolors[rs][cs])
1561+
# Calculate the minimal amount of padding that will allow us to extract
1562+
# vectorized patches.
1563+
rrem = (rows - 1) % rstride
1564+
rpadding = rstride - rrem if rrem != 0 else 0
1565+
crem = (cols - 1) % cstride
1566+
cpadding = cstride - crem if crem != 0 else 0
1567+
1568+
def pad_for_patches(a):
1569+
result = np.empty_like(a, shape=(rows + rpadding, cols + cpadding))
1570+
result[:rows, :cols] = a
1571+
if cpadding:
1572+
result[:rows, -cpadding:] = a[:, -1:]
1573+
if rpadding:
1574+
result[-rpadding:, :cols] = a[-1:, :]
1575+
result[rows:, cols:] = a[-1, -1]
1576+
return result
1577+
1578+
polys = np.stack(
1579+
[cbook._array_patch_perimeters(pad_for_patches(a),
1580+
rstride, cstride)
1581+
for a in (X, Y, Z)],
1582+
axis=-1)
1583+
if fcolors is not None:
1584+
colset = fcolors[np.ix_(np.arange(0, rows-1, rstride),
1585+
np.arange(0, cols-1, cstride))]
15881586

15891587
# note that the striding causes some polygons to have more coordinates
15901588
# than others

0 commit comments

Comments
 (0)