8000 Merge pull request #1298 from dmcdougall/update_trisurf · certik/matplotlib@89482b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 89482b2

Browse files
committed
Merge pull request matplotlib#1298 from dmcdougall/update_trisurf
Update mplot3d trisurf to support custom triangulations.
2 parents 0cdf79a + ee78a57 commit 89482b2

File tree

2 files changed

+91
-10
lines changed

2 files changed

+91
-10
lines changed

examples/mplot3d/trisurf3d_demo2.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from mpl_toolkits.mplot3d import Axes3D
4+
import matplotlib.tri as mtri
5+
6+
# u, v are parameterisation variables
7+
u = (np.linspace(0, 2.0 * np.pi, endpoint=True, num=50) * np.ones((10, 1))).flatten()
8+
v = np.repeat(np.linspace(-0.5, 0.5, endpoint=True, num=10), repeats=50).flatten()
9+
10+
# This is the Mobius mapping, taking a u, v pair and returning an x, y, z
11+
# triple
12+
x = (1 + 0.5 * v * np.cos(u / 2.0)) * np.cos(u)
13+
y = (1 + 0.5 * v * np.cos(u / 2.0)) * np.sin(u)
14+
z = 0.5 * v * np.sin(u / 2.0)
15+
16+
# Triangulate parameter space to determine the triangles
17+
tri = mtri.Triangulation(u, v)
18+
19+
fig = plt.figure()
20+
ax = fig.add_subplot(1, 1, 1, projection='3d')
21+
22+
# The triangles in parameter space determine which x, y, z points are
23+
# connected by an edge
24+
ax.plot_trisurf(x, y, z, triangles=tri.triangles, cmap=plt.cm.Spectral)
25+
26+
ax.set_zlim(-1, 1)
27+
28+
# First create the x and y coordinates of the points.
29+
n_angles = 36
30+
n_radii = 8
31+
min_radius = 0.25
32+
radii = np.linspace(min_radius, 0.95, n_radii)
33+
34+
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)
35+
angles = np.repeat(angles[...,np.newaxis], n_radii, axis=1)
36+
angles[:,1::2] += np.pi/n_angles
37+
38+
x = (radii*np.cos(angles)).flatten()
39+
y = (radii*np.sin(angles)).flatten()
40+
z = (np.cos(radii)*np.cos(angles*3.0)).flatten()
41+
42+
# Create the Triangulation; no triangles so Delaunay triangulation created.
43+
triang = mtri.Triangulation(x, y)
44+
45+
# Mask off unwanted triangles.
46+
xmid = x[triang.triangles].mean(axis=1)
47+
ymid = y[triang.triangles].mean(axis=1)
48+
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
49+
triang.set_mask(mask)
50+
51+
# tripcolor plot.
52+
fig = plt.figure()
53+
ax = fig.add_subplot(1, 1, 1, projection='3d')
54+
ax.plot_trisurf(triang, z, cmap=plt.cm.CMRmap)
55+
plt.show()

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ def plot_wireframe(self, X, Y, Z, *args, **kwargs):
15621562

15631563
return linec
15641564

1565-
def plot_trisurf(self, X, Y, Z, *args, **kwargs):
1565+
def plot_trisurf(self, *args, **kwargs):
15661566
"""
15671567
============= ================================================
15681568
Argument Description
@@ -1576,9 +1576,37 @@ def plot_trisurf(self, X, Y, Z, *args, **kwargs):
15761576
*shade* Whether to shade the facecolors
15771577
============= ================================================
15781578
1579+
The (optional) triangulation can be specified in one of two ways;
1580+
either::
1581+
1582+
plot_trisurf(triangulation, ...)
1583+
1584+
where triangulation is a :class:`~matplotlib.tri.Triangulation`
1585+
object, or::
1586+
1587+
plot_trisurf(X, Y, ...)
1588+
plot_trisurf(X, Y, triangles, ...)
1589+
plot_trisurf(X, Y, triangles=triangles, ...)
1590+
1591+
in which case a Triangulation object will be created. See
1592+
:class:`~matplotlib.tri.Triangulation` for a explanation of
1593+
these possibilities.
1594+
1595+
The remaining arguments are::
1596+
1597+
plot_trisurf(..., Z)
1598+
1599+
where *Z* is the array of values to contour, one per point
1600+
in the triangulation.
1601+
15791602
Other arguments are passed on to
15801603
:class:`~mpl_toolkits.mplot3d.art3d.Poly3DCollection`
15811604
1605+
**Examples:**
1606+
1607+
.. plot:: mpl_examples/mplot3d/trisurf3d_demo.py
1608+
.. plot:: mpl_examples/mplot3d/trisurf3d_demo2.py
1609+
15821610
.. versionadded:: 1.2.0
15831611
This plotting function was added for the v1.2.0 release.
15841612
"""
@@ -1596,15 +1624,13 @@ def plot_trisurf(self, X, Y, Z, *args, **kwargs):
15961624
shade = kwargs.pop('shade', cmap is None)
15971625
lightsource = kwargs.pop('lightsource', None)
15981626

1599-
# TODO: Support masked triangulations
1600-
tri = Triangulation(X, Y)
1601-
x = tri.x
1602-
y = tri.y
1603-
triangles = tri.triangles
1627+
tri, args, kwargs = Triangulation.get_from_args_and_kwargs(*args, **kwargs)
1628+
z = np.asarray(args[0])
16041629

1605-
xt = x[triangles][...,np.newaxis]
1606-
yt = y[triangles][...,np.newaxis]
1607-
zt = np.array(Z)[triangles][...,np.newaxis]
1630+
triangles = tri.get_masked_triangles()
1631+
xt = tri.x[triangles][...,np.newaxis]
1632+
yt = tri.y[triangles][...,np.newaxis]
1633+
zt = np.array(z)[triangles][...,np.newaxis]
16081634

16091635
verts = np.concatenate((xt, yt, zt), axis=2)
16101636

@@ -1649,7 +1675,7 @@ def plot_trisurf(self, X, Y, Z, *args, **kwargs):
16491675
polyc.set_facecolors(colset)
16501676

16511677
self.add_collection(polyc)
1652-
self.auto_scale_xyz(X, Y, Z, had_data)
1678+
self.auto_scale_xyz(tri.x, tri.y, z, had_data)
16531679

16541680
return polyc
16551681

0 commit comments

Comments
 (0)
0