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

Skip to content

Commit 17159dc

Browse files
committed
Make plot_trisurf support custom triangulations
This brings plot_trisurf more inline with the 2d tri* method equivalents. An example has also been added to: a) reflect the new support; and b) illustrate the additional flexibility this change enables.
1 parent 6b74c57 commit 17159dc

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 = (np.linspace(0, 2.0 * np.pi, endpoint=True, num=50) * np.ones((10, 1))).flatten()
7+
v = np.repeat(np.linspace(-0.5, 0.5, endpoint=True, num=10), repeats=50).flatten()
8+
9+
x = (1 + 0.5 * v * np.cos(u / 2.0)) * np.cos(u)
10+
y = (1 + 0.5 * v * np.cos(u / 2.0)) * np.sin(u)
11+
z = 0.5 * v * np.sin(u / 2.0)
12+
13+
tri = mtri.Triangulation(u, v)
14+
15+
fig = plt.figure()
16+
ax = fig.add_subplot(1, 1, 1, projection='3d')
17+
ax.plot_trisurf(x, y, z, triangles=tri.triangles)
18+
ax.set_zlim(-1, 1)
19+
plt.show()

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 8 additions & 9 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
@@ -1596,15 +1596,14 @@ def plot_trisurf(self, X, Y, Z, *args, **kwargs):
15961596
shade = kwargs.pop('shade', cmap is None)
15971597
lightsource = kwargs.pop('lightsource', None)
15981598

1599+
tri, args, kwargs = Triangulation.get_from_args_and_kwargs(*args, **kwargs)
1600+
z = np.asarray(args[0])
1601+
15991602
# TODO: Support masked triangulations
1600-
tri = Triangulation(X, Y)
1601-
x = tri.x
1602-
y = tri.y
16031603
triangles = tri.triangles
1604-
1605-
xt = x[triangles][...,np.newaxis]
1606-
yt = y[triangles][...,np.newaxis]
1607-
zt = np.array(Z)[triangles][...,np.newaxis]
1604+
xt = tri.x[triangles][...,np.newaxis]
1605+
yt = tri.y[triangles][...,np.newaxis]
1606+
zt = np.array(z)[triangles][...,np.newaxis]
16081607

16091608
verts = np.concatenate((xt, yt, zt), axis=2)
16101609

@@ -1649,7 +1648,7 @@ def plot_trisurf(self, X, Y, Z, *args, **kwargs):
16491648
polyc.set_facecolors(colset)
16501649

16511650
self.add_collection(polyc)
1652-
self.auto_scale_xyz(X, Y, Z, had_data)
1651+
self.auto_scale_xyz(tri.x, tri.y, z, had_data)
16531652

16541653
return polyc
16551654

0 commit comments

Comments
 (0)