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

Skip to content

Commit 885ce29

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

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

examples/mplot3d/surface3d_demo.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
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

15+
716
fig = plt.figure()
817
ax = fig.gca(projection='3d')
18+
19+
# Make data.
920
X = np.arange(-5, 5, 0.25)
1021
Y = np.arange(-5, 5, 0.25)
1122
X, Y = np.meshgrid(X, Y)
1223
R = np.sqrt(X**2 + Y**2)
1324
Z = np.sin(R)
25+
26+
# Plot the surface.
1427
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
1528
linewidth=0, antialiased=False)
16-
ax.set_zlim(-1.01, 1.01)
1729

30+
# Customize the z axis.
31+
ax.set_zlim(-1.01, 1.01)
1832
ax.zaxis.set_major_locator(LinearLocator(10))
1933
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
2034

35+
# Add a color bar which maps values to colors.
2136
fig.colorbar(surf, shrink=0.5, aspect=5)
2237

2338
plt.show()

examples/mplot3d/surface3d_demo2.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
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

9+
510
fig = plt.figure()
611
ax = fig.add_subplot(111, projection='3d')
712

13+
# Make data
814
u = np.linspace(0, 2 * np.pi, 100)
915
v = np.linspace(0, np.pi, 100)
10-
1116
x = 10 * np.outer(np.cos(u), np.sin(v))
1217
y = 10 * np.outer(np.sin(u), np.sin(v))
1318
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
19+
20+
# Plot the surface
1421
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
1522

1623
plt.show()

examples/mplot3d/surface3d_demo3.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
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

11+
712
fig = plt.figure()
813
ax = fig.gca(projection='3d')
14+
15+
# Make data.
916
X = np.arange(-5, 5, 0.25)
1017
xlen = len(X)
1118
Y = np.arange(-5, 5, 0.25)
@@ -14,16 +21,20 @@
1421
R = np.sqrt(X**2 + Y**2)
1522
Z = np.sin(R)
1623

24+
# Create an empty array of strings with the same shape as the meshgrid, and
25+
# populate it with two colors in a checkerboard pattern.
1726
colortuple = ('y', 'b')
1827
colors = np.empty(X.shape, dtype=str)
1928
for y in range(ylen):
2029
for x in range(xlen):
2130
colors[x, y] = colortuple[(x + y) % len(colortuple)]
2231

32+
# Plot the surface with face colors taken from the array we made.
2333
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors,
2434
linewidth=0)
2535

26-
ax.set_zlim3d(-1, 1)
36+
# Customize the z axis.
37+
ax.set_zlim(-1, 1)
2738
ax.w_zaxis.set_major_locator(LinearLocator(6))
2839

2940
plt.show()
Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
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
11+
import numpy as np
12+
13+
1014
fig = plt.figure()
1115
ax = fig.add_subplot(111, projection='3d')
1216

13-
# create supporting points in polar coordinates
17+
# Create the mesh in polar coordinates and compute corresponding Z.
1418
r = np.linspace(0, 1.25, 50)
1519
p = np.linspace(0, 2*np.pi, 50)
1620
R, P = np.meshgrid(r, p)
17-
# transform them to cartesian system
21+
Z = ((R**2 - 1)**2)
22+
23+
# Express the mesh in the cartesian system.
1824
X, Y = R*np.cos(P), R*np.sin(P)
1925

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)
26+
# Plot the surface.
27+
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.YlGnBu_r)
28+
29+
# Tweak the limits and add latex math labels.
30+
ax.set_zlim(0, 1)
2331
ax.set_xlabel(r'$\phi_\mathrm{real}$')
2432
ax.set_ylabel(r'$\phi_\mathrm{im}$')
2533
ax.set_zlabel(r'$V(\phi)$')
34+
2635
plt.show()

0 commit comments

Comments
 (0)