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

Skip to content

Commit 890e880

Browse files
committed
DOC Cleaning up mplot3d examples quiver3d_demo, rotate_axes3d_demo, scatter3d_demo, subplot3d_demo, and text3d_demo: Comments, docstrings and tweaks. [MEP12]
1 parent 885a82b commit 890e880

File tree

5 files changed

+72
-20
lines changed

5 files changed

+72
-20
lines changed

examples/mplot3d/quiver3d_demo.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
'''
2+
Demonstrates plotting directional arrows at points on a 3d meshgrid.
3+
'''
4+
15
from mpl_toolkits.mplot3d import axes3d
26
import matplotlib.pyplot as plt
37
import numpy as np
48

59
fig = plt.figure()
610
ax = fig.gca(projection='3d')
711

12+
# Make the grid
813
x, y, z = np.meshgrid(np.arange(-0.8, 1, 0.2),
914
np.arange(-0.8, 1, 0.2),
1015
np.arange(-0.8, 1, 0.8))
1116

17+
# Make the direction data for the arrows
1218
u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
1319
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
1420
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1+
'''
2+
A very simple animation of a rotating 3D plot.
3+
4+
See wire3d_animation_demo for another simple example of animating a 3D plot.
5+
'''
6+
17
from mpl_toolkits.mplot3d import axes3d
28
import matplotlib.pyplot as plt
3-
import numpy as np
4-
59

610
fig = plt.figure()
711
ax = fig.add_subplot(111, projection='3d')
12+
13+
# load some test data for demonstration and plot a wireframe
814
X, Y, Z = axes3d.get_test_data(0.1)
915
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)
1016

17+
# rotate the axes and update
1118
for angle in range(0, 360):
1219
ax.view_init(30, angle)
1320
plt.draw()
21+
plt.pause(.001)

examples/mplot3d/scatter3d_demo.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
import numpy as np
1+
'''
2+
Demonstration of a basic scatterplot in 3D.
3+
'''
4+
25
from mpl_toolkits.mplot3d import Axes3D
36
import matplotlib.pyplot as plt
7+
import numpy as np
48

59

610
def randrange(n, vmin, vmax):
11+
'''
12+
Helper function to make an array of random numbers having shape (n, )
13+
with each number distributed Uniform(vmin, vmax).
14+
'''
715
return (vmax - vmin)*np.random.rand(n) + vmin
816

917
fig = plt.figure()
1018
ax = fig.add_subplot(111, projection='3d')
19+
1120
n = 100
12-
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
21+
22+
# For each set of style and range settings, plot n random points in the box
23+
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
24+
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
1325
xs = randrange(n, 23, 32)
1426
ys = randrange(n, 0, 100)
15-
zs = randrange(n, zl, zh)
27+
zs = randrange(n, zlow, zhigh)
1628
ax.scatter(xs, ys, zs, c=c, marker=m)
1729

1830
ax.set_xlabel('X Label')

examples/mplot3d/subplot3d_demo.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
1-
from mpl_toolkits.mplot3d.axes3d import Axes3D
2-
import matplotlib.pyplot as plt
3-
1+
'''
2+
Demonstrate including 3D plots as subplots.
3+
'''
44

5-
# imports specific to the plots in this example
6-
import numpy as np
5+
import matplotlib.pyplot as plt
6+
from mpl_toolkits.mplot3d.axes3d import Axes3D, get_test_data
77
from matplotlib import cm
8-
from mpl_toolkits.mplot3d.axes3d import get_test_data
8+
import numpy as np
99

10-
# Twice as wide as it is tall.
10+
11+
# set up a figure twice as wide as it is tall
1112
fig = plt.figure(figsize=plt.figaspect(0.5))
1213

13-
#---- First subplot
14+
#===============
15+
# First subplot
16+
#===============
17+
# set up the axes for the first plot
1418
ax = fig.add_subplot(1, 2, 1, projection='3d')
19+
20+
# plot a 3D surface like in the example mplot3d/surface3d_demo
1521
X = np.arange(-5, 5, 0.25)
1622
Y = np.arange(-5, 5, 0.25)
1723
X, Y = np.meshgrid(X, Y)
1824
R = np.sqrt(X**2 + Y**2)
1925
Z = np.sin(R)
2026
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
2127
linewidth=0, antialiased=False)
22-
ax.set_zlim3d(-1.01, 1.01)
23-
28+
ax.set_zlim(-1.01, 1.01)
2429
fig.colorbar(surf, shrink=0.5, aspect=10)
2530

26-
#---- Second subplot
31+
#===============
32+
# Second subplot
33+
#===============
34+
# set up the axes for the second plot
2735
ax = fig.add_subplot(1, 2, 2, projection='3d')
36+
37+
# plot a 3D wireframe like in the example mplot3d/wire3d_demo
2838
X, Y, Z = get_test_data(0.05)
2939
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
3040

examples/mplot3d/text3d_demo.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
'''
2+
Demonstrates the placement of text annotations on a 3D plot.
3+
4+
Functionality shown:
5+
- Using the text function with three types of 'zdir' values: None,
6+
an axis name (ex. 'x'), or a direction tuple (ex. (1, 1, 0)).
7+
- Using the text function with the color keyword.
8+
- Using the text2D function to place text on a fixed position on the ax object.
9+
'''
10+
111
from mpl_toolkits.mplot3d import Axes3D
212
import matplotlib.pyplot as plt
313

14+
415
fig = plt.figure()
516
ax = fig.gca(projection='3d')
617

18+
# Demo 1: zdir
719
zdirs = (None, 'x', 'y', 'z', (1, 1, 0), (1, 1, 1))
820
xs = (1, 4, 4, 9, 4, 1)
921
ys = (2, 5, 8, 10, 1, 2)
@@ -13,13 +25,17 @@
1325
label = '(%d, %d, %d), dir=%s' % (x, y, z, zdir)
1426
ax.text(x, y, z, label, zdir)
1527

28+
# Demo 2: color
1629
ax.text(9, 0, 0, "red", color='red')
17-
ax.text2D(0.05, 0.95, "2D Text", transform=ax.transAxes)
1830

19-
ax.set_xlim3d(0, 10)
20-
ax.set_ylim3d(0, 10)
21-
ax.set_zlim3d(0, 10)
31+
# Demo 3: text2D
32+
# Placement 0, 0 would be the bottom left, 1, 1 would be the top right.
33+
ax.text2D(0.05, 0.95, "2D Text", transform=ax.transAxes)
2234

35+
# Tweaking display region and labels
36+
ax.set_xlim(0, 10)
37+
ax.set_ylim(0, 10)
38+
ax.set_zlim(0, 10)
2339
ax.set_xlabel('X axis')
2440
ax.set_ylabel('Y axis')
2541
ax.set_zlabel('Z axis')

0 commit comments

Comments
 (0)