Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e36bb0b

Browse filesBrowse 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 e36bb0b
Copy full SHA for e36bb0b

File tree

Expand file treeCollapse file tree

3 files changed

+32
-30
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+32
-30
lines changed

‎lib/mpl_toolkits/mplot3d/axes3d.py

Copy file name to clipboardExpand all lines: lib/mpl_toolkits/mplot3d/axes3d.py
+13-30Lines 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
Loading

‎lib/mpl_toolkits/tests/test_mplot3d.py

Copy file name to clipboardExpand all lines: lib/mpl_toolkits/tests/test_mplot3d.py
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,25 @@ def test_trisurf3d():
216216
ax.plot_trisurf(x, y, z, cmap=cm.jet, linewidth=0.2)
217217

218218

219+
@image_comparison(baseline_images=['trisurf3d_shaded'], remove_text=True,
220+
tol=0.03, extensions=['png'])
221+
def test_trisurf3d_shaded():
222+
n_angles = 36
223+
n_radii = 8
224+
radii = np.linspace(0.125, 1.0, n_radii)
225+
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)
226+
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
227+
angles[:, 1::2] += np.pi/n_angles
228+
229+
x = np.append(0, (radii*np.cos(angles)).flatten())
230+
y = np.append(0, (radii*np.sin(angles)).flatten())
231+
z = np.sin(-x*y)
232+
233+
fig = plt.figure()
234+
ax = fig.gca(projection='3d')
235+
ax.plot_trisurf(x, y, z, color=[1, 0.5, 0], linewidth=0.2)
236+
237+
219238
@image_comparison(baseline_images=['wireframe3d'], remove_text=True)
220239
def test_wireframe3d():
221240
fig = plt.figure()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.