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

Skip to content

Commit e83ddaa

Browse files
committed
Add support for masked triangulations
An example has also been added (adapted from tripcolor) to illustrate the use of masked triangulations.
1 parent bd6f52a commit e83ddaa

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

examples/mplot3d/trisurf3d_demo2.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,36 @@
2121

2222
# The triangles in parameter space determnine which x, y, z points are
2323
# connected by an edge
24-
ax.plot_trisurf(x, y, z, triangles=tri.triangles)
24+
ax.plot_trisurf(x, y, z, triangles=tri.triangles, cmap=plt.cm.Spectral)
2525

2626
ax.set_zlim(-1, 1)
2727
plt.show()
28+
29+
# First create the x and y coordinates of the points.
30+
n_angles = 36
31+
n_radii = 8
32+
min_radius = 0.25
33+
radii = np.linspace(min_radius, 0.95, n_radii)
34+
35+
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)
36+
angles = np.repeat(angles[...,np.newaxis], n_radii, axis=1)
37+
angles[:,1::2] += np.pi/n_angles
38+
39+
x = (radii*np.cos(angles)).flatten()
40+
y = (radii*np.sin(angles)).flatten()
41+
z = (np.cos(radii)*np.cos(angles*3.0)).flatten()
42+
43+
# Create the Triangulation; no triangles so Delaunay triangulation created.
44+
triang = mtri.Triangulation(x, y)
45+
46+
# Mask off unwanted triangles.
47+
xmid = x[triang.triangles].mean(axis=1)
48+
ymid = y[triang.triangles].mean(axis=1)
49+
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
50+
triang.set_mask(mask)
51+
52+
# tripcolor plot.
53+
fig = plt.figure()
54+
ax = fig.add_subplot(1, 1, 1, projection='3d')
55+
ax.plot_trisurf(triang, z, cmap=plt.cm.CMRmap)
56+
plt.show()

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ def plot_trisurf(self, *args, **kwargs):
16281628
z = np.asarray(args[0])
16291629

16301630
# TODO: Support masked triangulations
1631-
triangles = tri.triangles
1631+
triangles = tri.get_masked_triangles()
16321632
xt = tri.x[triangles][...,np.newaxis]
16331633
yt = tri.y[triangles][...,np.newaxis]
16341634
zt = np.array(z)[triangles][...,np.newaxis]

0 commit comments

Comments
 (0)