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 6a5def2

Browse filesBrowse files
committed
DOC Cleaning up mplot3d/tri* examples: comments/docstrings, tweaks, and refactoring. Notably, viewing angles on tricontour examples are tweaked and the graphs of trisurf3d_demo2 are made into subplots. [MEP12]
1 parent 59695ff commit 6a5def2
Copy full SHA for 6a5def2

File tree

Expand file treeCollapse file tree

4 files changed

+81
-43
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+81
-43
lines changed

‎examples/mplot3d/tricontour3d_demo.py

Copy file name to clipboard
+16-9Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
"""
22
Contour plots of unstructured triangular grids.
3+
4+
The data used is the same as in the second plot of trisurf3d_demo2.
5+
tricontourf3d_demo shows the filled version of this example.
36
"""
7+
48
import matplotlib.pyplot as plt
59
from mpl_toolkits.mplot3d import Axes3D
610
import matplotlib.tri as tri
711
import numpy as np
8-
import math
912

10-
# First create the x and y coordinates of the points.
1113
n_angles = 48
1214
n_radii = 8
1315
min_radius = 0.25
14-
radii = np.linspace(min_radius, 0.95, n_radii)
1516

16-
angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
17+
# Create the mesh in polar coordinates and compute x, y, z.
18+
radii = np.linspace(min_radius, 0.95, n_radii)
19+
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)
1720
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
18-
angles[:, 1::2] += math.pi/n_angles
21+
angles[:, 1::2] += np.pi/n_angles
1922

2023
x = (radii*np.cos(angles)).flatten()
2124
y = (radii*np.sin(angles)).flatten()
2225
z = (np.cos(radii)*np.cos(angles*3.0)).flatten()
2326

24-
# Create a custom triangulation
27+
# Create a custom triangulation.
2528
triang = tri.Triangulation(x, y)
2629

2730
# Mask off unwanted triangles.
@@ -30,7 +33,11 @@
3033
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
3134
triang.set_mask(mask)
3235

33-
plt.figure()
34-
plt.gca(projection='3d')
35-
plt.tricontour(triang, z)
36+
fig = plt.figure()
37+
ax = fig.gca(projection='3d')
38+
ax.tricontour(triang, z, cmap=plt.cm.CMRmap)
39+
40+
# Customize the view angle so it's easier to understand the plot.
41+
ax.view_init(elev=45.)
42+
3643
plt.show()
+18-10Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
"""
2-
Contour plots of unstructured triangular grids.
2+
Filled contour plots of unstructured triangular grids.
3+
4+
The data used is the same as in the second plot of trisurf3d_demo2.
5+
tricontour3d_demo shows the unfilled version of this example.
36
"""
7+
48
import matplotlib.pyplot as plt
59
from mpl_toolkits.mplot3d import Axes3D
610
import matplotlib.tri as tri
711
import numpy as np
8-
import math
912

10-
# First create the x and y coordinates of the points.
13+
# First create the x, y, z coordinates of the points.
1114
n_angles = 48
1215
n_radii = 8
1316
min_radius = 0.25
14-
radii = np.linspace(min_radius, 0.95, n_radii)
1517

16-
angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
18+
# Create the mesh in polar coordinates and compute x, y, z.
19+
radii = np.linspace(min_radius, 0.95, n_radii)
20+
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)
1721
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
18-
angles[:, 1::2] += math.pi/n_angles
22+
angles[:, 1::2] += np.pi/n_angles
1923

2024
x = (radii*np.cos(angles)).flatten()
2125
y = (radii*np.sin(angles)).flatten()
2226
z = (np.cos(radii)*np.cos(angles*3.0)).flatten()
2327

24-
# Create a custom triangulation
28+
# Create a custom triangulation.
2529
triang = tri.Triangulation(x, y)
2630

2731
# Mask off unwanted triangles.
@@ -30,7 +34,11 @@
3034
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
3135
triang.set_mask(mask)
3236

33-
plt.figure()
34-
plt.gca(projection='3d')
35-
plt.tricontourf(triang, z)
37+
fig = plt.figure()
38+
ax = fig.gca(projection='3d')
39+
ax.tricontourf(triang, z, cmap=plt.cm.CMRmap)
40+
41+
# Customize the view angle so it's easier to understand the plot.
42+
ax.view_init(elev=45.)
43+
3644
plt.show()

‎examples/mplot3d/trisurf3d_demo.py

Copy file name to clipboardExpand all lines: examples/mplot3d/trisurf3d_demo.py
+12-10Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1+
'''
2+
Plot a 3D surface with a triangular mesh.
3+
'''
4+
15
from mpl_toolkits.mplot3d import Axes3D
2-
from matplotlib import cm
36
import matplotlib.pyplot as plt
47
import numpy as np
58

6-
n_angles = 36
9+
710
n_radii = 8
11+
n_angles = 36
812

9-
# An array of radii
10-
# Does not include radius r=0, this is to eliminate duplicate points
13+
# Make radii and angles spaces (radius r=0 omitted to eliminate duplication).
1114
radii = np.linspace(0.125, 1.0, n_radii)
12-
13-
# An array of angles
1415
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)
1516

16-
# Repeat all angles for each radius
17+
# Repeat all angles for each radius.
1718
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
1819

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
20+
# Convert polar (radii, angles) coords to cartesian (x, y) coords.
21+
# (0, 0) is manually added at this stage, so there will be no duplicate
22+
# points in the (x, y) plane.
2123
x = np.append(0, (radii*np.cos(angles)).flatten())
2224
y = np.append(0, (radii*np.sin(angles)).flatten())
2325

24-
# Pringle surface
26+
# Compute z to make the pringle surface.
2527
z = np.sin(-x*y)
2628

2729
fig = plt.figure()

‎examples/mplot3d/trisurf3d_demo2.py

Copy file name to clipboard
+35-14Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1+
'''
2+
Two additional examples of plotting surfaces with triangular mesh.
3+
4+
The first demonstrates use of plot_trisurf's triangles argument, and the
5+
second sets a Triangulation object's mask and passes the object directly
6+
to plot_trisurf.
7+
'''
8+
19
import numpy as np
210
import matplotlib.pyplot as plt
311
from mpl_toolkits.mplot3d import Axes3D
412
import matplotlib.tri as mtri
513

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()
14+
15+
fig = plt.figure(figsize=plt.figaspect(0.5))
16+
17+
#============
18+
# First plot
19+
#============
20+
21+
# Make a mesh in the space of parameterisation variables u and v
22+
u = np.linspace(0, 2.0 * np.pi, endpoint=True, num=50)
23+
v = np.linspace(-0.5, 0.5, endpoint=True, num=10)
24+
u, v = np.meshgrid(u, v)
25+
u, v = u.flatten(), v.flatten()
926

1027
# This is the Mobius mapping, taking a u, v pair and returning an x, y, z
1128
# triple
@@ -16,16 +33,18 @@
1633
# Triangulate parameter space to determine the triangles
1734
tri = mtri.Triangulation(u, v)
1835

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
36+
# Plot the surface. The triangles in parameter space determine which x, y, z
37+
# points are connected by an edge.
38+
ax = fig.add_subplot(1, 2, 1, projection='3d')
2439
ax.plot_trisurf(x, y, z, triangles=tri.triangles, cmap=plt.cm.Spectral)
25-
2640
ax.set_zlim(-1, 1)
2741

28-
# First create the x and y coordinates of the points.
42+
43+
#============
44+
# Second plot
45+
#============
46+
47+
# Make parameter spaces radii and angles.
2948
n_angles = 36
3049
n_radii = 8
3150
min_radius = 0.25
@@ -35,6 +54,7 @@
3554
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
3655
angles[:, 1::2] += np.pi/n_angles
3756

57+
# Map radius, angle pairs to x, y, z points.
3858
x = (radii*np.cos(angles)).flatten()
3959
y = (radii*np.sin(angles)).flatten()
4060
z = (np.cos(radii)*np.cos(angles*3.0)).flatten()
@@ -45,11 +65,12 @@
4565
# Mask off unwanted triangles.
4666
xmid = x[triang.triangles].mean(axis=1)
4767
ymid = y[triang.triangles].mean(axis=1)
48-
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
68+
mask = np.where(xmid**2 + ymid**2 < min_radius**2, 1, 0)
4969
triang.set_mask(mask)
5070

51-
# tripcolor plot.
52-
fig = plt.figure()
53-
ax = fig.add_subplot(1, 1, 1, projection='3d')
71+
# Plot the surface.
72+
ax = fig.add_subplot(1, 2, 2, projection='3d')
5473
ax.plot_trisurf(triang, z, cmap=plt.cm.CMRmap)
74+
75+
5576
plt.show()

0 commit comments

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