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

Skip to content

Commit 391f509

Browse files
committed
Merge branch 'dmcdouggall-trisurf'
2 parents 2822e34 + 9941d0d commit 391f509

5 files changed

Lines changed: 150 additions & 0 deletions

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
2012-02-29 errorevery keyword added to errorbar to enable errorbar
22
subsampling. fixes issue #600.
33

4+
2012-02-28 Added plot_trisurf to the mplot3d toolkit. This supports plotting
5+
three dimensional surfaces on an irregular grid. - Damon McDougall
6+
47
2012-01-23 The radius labels in polar plots no longer use a fixed
58
padding, but use a different alignment depending on the
69
quadrant they are in. This fixes numerical problems when

doc/mpl_toolkits/mplot3d/tutorial.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ Surface plots
6464
.. plot:: mpl_examples/mplot3d/surface3d_demo2.py
6565
.. plot:: mpl_examples/mplot3d/surface3d_demo3.py
6666

67+
.. _trisurface:
68+
69+
Tri-Surface plots
70+
=================
71+
.. automethod:: Axes3D.plot_trisurface
72+
73+
.. plot:: mpl_examples/mplot3d/trisurf3d_demo.py
74+
75+
6776
.. _contour3d:
6877

6978
Contour plots

doc/users/whats_new.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ This page just covers the highlights -- for the full story, see the
1212
versions 2.4 to 2.7. matplotlib 1.2 and later require
1313
versions 2.6, 2.7, and 3.1 and higher.
1414

15+
.. _whats-new-1-2:
16+
17+
new in matplotlib-1.2
18+
=====================
19+
20+
Tri-Surface Plots
21+
-----------------
22+
23+
Damon McDougall added a new plotting method for the
24+
:mod:`~mpl_toolkits.mplot3d` toolkit called
25+
:meth:`~mpl_toolkits.mplot3d.axes3d.Axes3D.plot_trisurf`.
26+
27+
.. plot:: mpl_examples/mplot3d/trisurf3d_demo.py
28+
1529
.. _whats-new-1-1:
1630

1731
new in matplotlib-1.1

examples/mplot3d/trisurf3d_demo.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from mpl_toolkits.mplot3d import Axes3D
2+
from matplotlib import cm
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
6+
n_angles = 36
7+
n_radii = 8
8+
9+
# An array of radii
10+
# Does not include radius r=0, this is to eliminate duplicate points
11+
radii = np.linspace(0.125, 1.0, n_radii)
12+
13+
# An array of angles
14+
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)
15+
16+
# Repeat all angles for each radius
17+
angles = np.repeat(angles[...,np.newaxis], n_radii, axis=1)
18+
19+
# Convert polar (radii, angles) coords to cartesian (x, y) coords
20+
# (0, 0) is added here. There are no duplicate points in the (x, y) plane
21+
x = np.append(0, (radii*np.cos(angles)).flatten())
22+
y = np.append(0, (radii*np.sin(angles)).flatten())
23+
24+
# Pringle surface
25+
z = np.sin(-x*y)
26+
27+
fig = plt.figure()
28+
ax = fig.gca(projection='3d')
29+
30+
ax.plot_trisurf(x, y, z, cmap=cm.jet, linewidth=0.2)
31+
32+
plt.show()

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import matplotlib.collections as mcoll
2020
from matplotlib import docstring
2121
import matplotlib.scale as mscale
22+
from matplotlib.tri.triangulation import Triangulation
2223
import numpy as np
2324
from matplotlib.colors import Normalize, colorConverter, LightSource
2425

@@ -1570,6 +1571,97 @@ def plot_wireframe(self, X, Y, Z, *args, **kwargs):
15701571

15711572
return linec
15721573

1574+
def plot_trisurf(self, X, Y, Z, *args, **kwargs):
1575+
"""
1576+
============= ================================================
1577+
Argument Description
1578+
============= ================================================
1579+
*X*, *Y*, *Z* Data values as 1D arrays
1580+
*color* Color of the surface patches
1581+
*cmap* A colormap for the surface patches.
1582+
*norm* An instance of Normalize to map values to colors
1583+
*vmin* Minimum value to map
1584+
*vmax* Maximum value to map
1585+
*shade* Whether to shade the facecolors
1586+
============= ================================================
1587+
1588+
Other arguments are passed on to
1589+
:class:`~mpl_toolkits.mplot3d.art3d.Poly3DCollection`
1590+
1591+
.. versionadded:: 1.2.0
1592+
This plotting function was added for the v1.2.0 release.
1593+
"""
1594+
1595+
had_data = self.has_data()
1596+
1597+
# TODO: Support custom face colours
1598+
color = np.array(colorConverter.to_rgba(kwargs.pop('color', 'b')))
1599+
1600+
cmap = kwargs.get('cmap', None)
1601+
norm = kwargs.pop('norm', None)
1602+
vmin = kwargs.pop('vmin', None)
1603+
vmax = kwargs.pop('vmax', None)
1604+
linewidth = kwargs.get('linewidth', None)
1605+
shade = kwargs.pop('shade', cmap is None)
1606+
lightsource = kwargs.pop('lightsource', None)
1607+
1608+
# TODO: Support masked triangulations
1609+
tri = Triangulation(X, Y)
1610+
x = tri.x
1611+
y = tri.y
1612+
triangles = tri.triangles
1613+
1614+
xt = x[triangles][...,np.newaxis]
1615+
yt = y[triangles][...,np.newaxis]
1616+
zt = np.array(Z)[triangles][...,np.newaxis]
1617+
1618+
verts = np.concatenate((xt, yt, zt), axis=2)
1619+
1620+
# Only need these vectors to shade if there is no cmap
1621+
if cmap is None and shade:
1622+
totpts = len(verts)
1623+
v1 = np.empty((totpts, 3))
1624+
v2 = np.empty((totpts, 3))
1625+
# This indexes the vertex points
1626+
which_pt = 0
1627+
1628+
colset = []
1629+
for i in xrange(len(verts)):
1630+
avgzsum = verts[i,0,2] + verts[i,1,2] + verts[i,2,2]
1631+
colset.append(avgzsum / 3.0)
1632+
1633+
# Only need vectors to shade if no cmap
1634+
if cmap is None and shade:
1635+
v1[which_pt] = np.array(verts[i,0]) - np.array(verts[i,1])
1636+
v2[which_pt] = np.array(verts[i,1]) - np.array(verts[i,2])
1637+
which_pt += 1
1638+
1639+
if cmap is None and shade:
1640+
normals = np.cross(v1, v2)
1641+
else:
1642+
normals = []
1643+
1644+
polyc = art3d.Poly3DCollection(verts, *args, **kwargs)
1645+
1646+
if cmap:
1647+
colset = np.array(colset)
1648+
polyc.set_array(colset)
1649+
if vmin is not None or vmax is not None:
1650+
polyc.set_clim(vmin, vmax)
1651+
if norm is not None:
1652+
polyc.set_norm(norm)
1653+
else:
1654+
if shade:
1655+
colset = self._shade_colors(color, normals)
1656+
else:
1657+
colset = color
1658+
polyc.set_facecolors(colset)
1659+
1660+
self.add_collection(polyc)
1661+
self.auto_scale_xyz(X, Y, Z, had_data)
1662+
1663+
return polyc
1664+
15731665
def _3d_extend_contour(self, cset, stride=5):
15741666
'''
15751667
Extend a contour in 3D by creating

0 commit comments

Comments
 (0)