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

Skip to content

Commit cb05203

Browse files
committed
MAINT: Use vectorization in plot_trisurf
Use it to compute surface normals and mean z coord, rather than manual looping
1 parent 024d423 commit cb05203

File tree

1 file changed

+13
-30
lines changed

1 file changed

+13
-30
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,47 +1984,30 @@ def plot_trisurf(self, *args, **kwargs):
19841984
args = args[1:]
19851985

19861986
triangles = tri.get_masked_triangles()
1987-
xt = tri.x[triangles][..., np.newaxis]
1988-
yt = tri.y[triangles][..., np.newaxis]
1989-
zt = z[triangles][..., np.newaxis]
1987+
xt = tri.x[triangles]
1988+
yt = tri.y[triangles]
1989+
zt = z[triangles]
19901990

1991-
verts = np.concatenate((xt, yt, zt), axis=2)
1992-
1993-
# Only need these vectors to shade if there is no cmap
1994-
if cmap is None and shade:
1995-
totpts = len(verts)
1996-
v1 = np.empty((totpts, 3))
1997-
v2 = np.empty((totpts, 3))
1998-
# This indexes the vertex points
1999-
which_pt = 0
2000-
2001-
colset = []
2002-
for i in xrange(len(verts)):
2003-
avgzsum = verts[i,0,2] + verts[i,1,2] + verts[i,2,2]
2004-
colset.append(avgzsum / 3.0)
2005-
2006-
# Only need vectors to shade if no cmap
2007-
if cmap is None and shade:
2008-
v1[which_pt] = np.array(verts[i,0]) - np.array(verts[i,1])
2009-
v2[which_pt] = np.array(verts[i,1]) - np.array(verts[i,2])
2010-
which_pt += 1
2011-
2012-
if cmap is None and shade:
2013-
normals = np.cross(v1, v2)
2014-
else:
2015-
normals = []
1991+
# verts = np.stack((xt, yt, zt), axis=-1)
1992+
verts = np.concatenate((
1993+
xt[..., np.newaxis], yt[..., np.newaxis], zt[..., np.newaxis]
1994+
), axis=-1)
20161995

20171996
polyc = art3d.Poly3DCollection(verts, *args, **kwargs)
20181997

20191998
if cmap:
2020-
colset = np.array(colset)
2021-
polyc.set_array(colset)
1999+
# average over the three points of each triangle
2000+
avg_z = verts[:, :, 2].mean(axis=1)
2001+
polyc.set_array(avg_z)
20222002
if vmin is not None or vmax is not None:
20232003
polyc.set_clim(vmin, vmax)
20242004
if norm is not None:
20252005
polyc.set_norm(norm)
20262006
else:
20272007
if shade:
2008+
v1 = verts[:, 0, :] - verts[:, 1, :]
2009+
v2 = verts[:, 1, :] - verts[:, 2, :]
2010+
normals = np.cross(v1, v2)
20282011
colset = self._shade_colors(color, normals)
20292012
else:
20302013
colset = color

0 commit comments

Comments
 (0)