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

Skip to content

Commit 4d99ff2

Browse files
committed
DOC Cleaning up mplot3d/surface3d* examples: comments/docstrings, tweaks, and refactoring. [MEP12]
1 parent 9868f03 commit 4d99ff2

File tree

4 files changed

+62
-24
lines changed

4 files changed

+62
-24
lines changed

examples/mplot3d/surface3d_demo.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
1+
'''
2+
Demonstrates plotting a 3D surface colored with the coolwarm color map.
3+
The surface is made opaque by using antialiased=False.
4+
5+
Also demonstrates using the LinearLocator and custom formatting for the
6+
z axis tick labels.
7+
'''
8+
19
from mpl_toolkits.mplot3d import Axes3D
10+
import matplotlib.pyplot as plt
211
from matplotlib import cm
312
from matplotlib.ticker import LinearLocator, FormatStrFormatter
4-
import matplotlib.pyplot as plt
513
import numpy as np
614

7-
fig = plt.figure()
8-
ax = fig.gca(projection='3d')
15+
16+
# Make data.
917
X = np.arange(-5, 5, 0.25)
1018
Y = np.arange(-5, 5, 0.25)
1119
X, Y = np.meshgrid(X, Y)
1220
R = np.sqrt(X**2 + Y**2)
1321
Z = np.sin(R)
22+
23+
# Plot the surface.
24+
fig = plt.figure()
25+
ax = fig.gca(projection='3d')
1426
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
1527
linewidth=0, antialiased=False)
16-
ax.set_zlim(-1.01, 1.01)
1728

29+
# Customize the z axis.
30+
ax.set_zlim(-1.01, 1.01)
1831
ax.zaxis.set_major_locator(LinearLocator(10))
1932
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
2033

34+
# Add a color bar which maps values to colors.
2135
fig.colorbar(surf, shrink=0.5, aspect=5)
2236

2337
plt.show()

examples/mplot3d/surface3d_demo2.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
'''
2+
Demonstrates a very basic plot of a 3D surface using a solid color.
3+
'''
4+
15
from mpl_toolkits.mplot3d import Axes3D
26
import matplotlib.pyplot as plt
37
import numpy as np
48

5-
fig = plt.figure()
6-
ax = fig.add_subplot(111, projection='3d')
79

10+
# Make data
811
u = np.linspace(0, 2 * np.pi, 100)
912
v = np.linspace(0, np.pi, 100)
10-
1113
x = 10 * np.outer(np.cos(u), np.sin(v))
1214
y = 10 * np.outer(np.sin(u), np.sin(v))
1315
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
16+
17+
# Plot the surface
18+
fig = plt.figure()
19+
ax = fig.add_subplot(111, projection='3d')
1420
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
1521

1622
plt.show()

examples/mplot3d/surface3d_demo3.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
'''
2+
Demonstrates plotting a 3D surface colored in a checkerboard pattern.
3+
'''
4+
15
from mpl_toolkits.mplot3d import Axes3D
6+
import matplotlib.pyplot as plt
27
from matplotlib import cm
38
from matplotlib.ticker import LinearLocator
4-
import matplotlib.pyplot as plt
59
import numpy as np
610

7-
fig = plt.figure()
8-
ax = fig.gca(projection='3d')
11+
12+
# Make data.
913
X = np.arange(-5, 5, 0.25)
1014
xlen = len(X)
1115
Y = np.arange(-5, 5, 0.25)
@@ -14,16 +18,22 @@
1418
R = np.sqrt(X**2 + Y**2)
1519
Z = np.sin(R)
1620

21+
# Create an empty array of strings with the same shape as the meshgrid, and
22+
# populate it with two colors in a checkerboard pattern.
1723
colortuple = ('y', 'b')
1824
colors = np.empty(X.shape, dtype=str)
1925
for y in range(ylen):
2026
for x in range(xlen):
2127
colors[x, y] = colortuple[(x + y) % len(colortuple)]
2228

29+
# Plot the surface with face colors taken from the array we made.
30+
fig = plt.figure()
31+
ax = fig.gca(projection='3d')
2332
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors,
2433
linewidth=0)
2534

26-
ax.set_zlim3d(-1, 1)
35+
# Customize the z axis.
36+
ax.set_zlim(-1, 1)
2737
ax.w_zaxis.set_major_locator(LinearLocator(6))
2838

2939
plt.show()
Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1-
# By Armin Moser
1+
'''
2+
Demonstrates plotting a surface defined in polar coordinates.
3+
Uses the reversed version of the YlGnBu color map.
4+
Also demonstrates writing axis labels with latex math mode.
5+
6+
Example contributed by Armin Moser.
7+
'''
28

39
from mpl_toolkits.mplot3d import Axes3D
4-
import matplotlib
5-
import numpy as np
6-
from matplotlib import cm
710
from matplotlib import pyplot as plt
8-
step = 0.04
9-
maxval = 1.0
10-
fig = plt.figure()
11-
ax = fig.add_subplot(111, projection='3d')
11+
import numpy as np
12+
1213

13-
# create supporting points in polar coordinates
14+
# Create the mesh in polar coordinates and compute corresponding Z.
1415
r = np.linspace(0, 1.25, 50)
1516
p = np.linspace(0, 2*np.pi, 50)
1617
R, P = np.meshgrid(r, p)
17-
# transform them to cartesian system
18+
Z = ((R**2 - 1)**2)
19+
20+
# Express the mesh in the cartesian system.
1821
X, Y = R*np.cos(P), R*np.sin(P)
1922

20-
Z = ((R**2 - 1)**2)
21-
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.YlGnBu_r)
22-
ax.set_zlim3d(0, 1)
23+
# Plot the surface.
24+
fig = plt.figure()
25+
ax = fig.add_subplot(111, projection='3d')
26+
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.YlGnBu_r)
27+
28+
# Tweak the limits and add latex math labels.
29+
ax.set_zlim(0, 1)
2330
ax.set_xlabel(r'$\phi_\mathrm{real}$')
2431
ax.set_ylabel(r'$\phi_\mathrm{im}$')
2532
ax.set_zlabel(r'$V(\phi)$')
33+
2634
plt.show()

0 commit comments

Comments
 (0)