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 f00db67

Browse filesBrowse files
committed
Merge pull request #6303 from discardthree/mplot3d-examples-MEP12
DOC Clean up on about half the Mplot3d examples
2 parents 7beca5d + ab0d48c commit f00db67
Copy full SHA for f00db67
Expand file treeCollapse file tree

14 files changed

+228
-78
lines changed
+23-4Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
1+
'''
2+
Demonstrates using ax.plot's zdir keyword to plot 2D scatterplot data on
3+
selective axes of a 3D plot.
4+
'''
5+
16
from mpl_toolkits.mplot3d import Axes3D
27
import numpy as np
38
import matplotlib.pyplot as plt
49

510
fig = plt.figure()
611
ax = fig.gca(projection='3d')
712

13+
# Plot a sin curve using the x and y axes.
814
x = np.linspace(0, 1, 100)
915
y = np.sin(x * 2 * np.pi) / 2 + 0.5
10-
ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')
16+
ax.plot(x, y, zs=0, zdir='z', label='curve in (x,y)')
1117

18+
# Plot scatterplot data (20 2D points per colour) on the x and z axes.
1219
colors = ('r', 'g', 'b', 'k')
20+
x = np.random.sample(20*len(colors))
21+
y = np.random.sample(20*len(colors))
22+
c_list = []
1323
for c in colors:
14-
x = np.random.sample(20)
15-
y = np.random.sample(20)
16-
ax.scatter(x, y, 0, zdir='y', c=c)
24+
c_list.append([c]*20)
25+
# By using zdir='y', the y value of these points is fixed to the zs value 0
26+
# and the (x,y) points are plotted on the x and z axes.
27+
ax.scatter(x, y, zs=0, zdir='y', c=c_list, label='points in (x,z)')
1728

29+
# Make legend, set axes limits and labels
1830
ax.legend()
1931
ax.set_xlim3d(0, 1)
2032
ax.set_ylim3d(0, 1)
2133
ax.set_zlim3d(0, 1)
34+
ax.set_xlabel('X')
35+
ax.set_ylabel('Y')
36+
ax.set_zlabel('Z')
37+
38+
# Customize the view angle so it's easier to see that the scatter points lie
39+
# on the plane y=0
40+
ax.view_init(elev=20., azim=-35)
2241

2342
plt.show()

‎examples/mplot3d/bars3d_demo.py

Copy file name to clipboard
+18-4Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
1+
'''
2+
Demonstrates making a 3D plot which has 2D bar graphs projected onto
3+
planes y=0, y=1, etc.
4+
'''
5+
16
from mpl_toolkits.mplot3d import Axes3D
27
import matplotlib.pyplot as plt
38
import numpy as np
49

510
fig = plt.figure()
611
ax = fig.add_subplot(111, projection='3d')
7-
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
12+
13+
colors = ['r', 'g', 'b', 'y']
14+
yticks = [3, 2, 1, 0]
15+
for c, k in zip(colors, yticks):
16+
# Generate the random data for the y=k 'layer'.
817
xs = np.arange(20)
918
ys = np.random.rand(20)
1019

11-
# You can provide either a single color or an array. To demonstrate this,
12-
# the first bar of each set will be colored cyan.
20+
# You can provide either a single color or an array with the same length as
21+
# xs and ys. To demonstrate this, we color the first bar of each set cyan.
1322
cs = [c] * len(xs)
1423
cs[0] = 'c'
15-
ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)
24+
25+
# Plot the bar graph given by xs and ys on the plane y=k with 80% opacity.
26+
ax.bar(xs, ys, zs=k, zdir='y', color=cs, alpha=0.8)
1627

1728
ax.set_xlabel('X')
1829
ax.set_ylabel('Y')
1930
ax.set_zlabel('Z')
2031

32+
# On the y axis let's only label the discrete values that we have data for.
33+
ax.set_yticks(yticks)
34+
2135
plt.show()

‎examples/mplot3d/contour3d_demo.py

Copy file name to clipboard
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
'''
2+
Demonstrates plotting contour (level) curves in 3D.
3+
4+
This is like a contour plot in 2D except that the f(x,y)=c curve is plotted
5+
on the plane z=c.
6+
'''
7+
18
from mpl_toolkits.mplot3d import axes3d
29
import matplotlib.pyplot as plt
310
from matplotlib import cm
411

512
fig = plt.figure()
6-
ax = fig.add_subplot(111, projection='3d')
13+
ax = fig.gca(projection='3d')
714
X, Y, Z = axes3d.get_test_data(0.05)
15+
16+
# Plot contour curves
817
cset = ax.contour(X, Y, Z, cmap=cm.coolwarm)
18+
919
ax.clabel(cset, fontsize=9, inline=1)
1020

1121
plt.show()

‎examples/mplot3d/contour3d_demo2.py

Copy file name to clipboard
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
'''
2+
Demonstrates plotting contour (level) curves in 3D using the extend3d option.
3+
4+
This modification of the contour3d_demo example uses extend3d=True to
5+
extend the curves vertically into 'ribbons'.
6+
'''
7+
18
from mpl_toolkits.mplot3d import axes3d
29
import matplotlib.pyplot as plt
310
from matplotlib import cm
411

512
fig = plt.figure()
613
ax = fig.gca(projection='3d')
714
X, Y, Z = axes3d.get_test_data(0.05)
15+
816
cset = ax.contour(X, Y, Z, extend3d=True, cmap=cm.coolwarm)
17+
918
ax.clabel(cset, fontsize=9, inline=1)
1019

1120
plt.show()

‎examples/mplot3d/contour3d_demo3.py

Copy file name to clipboard
+17-3Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
1+
'''
2+
Demonstrates displaying a 3D surface while also projecting contour 'profiles'
3+
onto the 'walls' of the graph.
4+
5+
See contourf3d_demo2 for the filled version.
6+
'''
7+
18
from mpl_toolkits.mplot3d import axes3d
29
import matplotlib.pyplot as plt
310
from matplotlib import cm
411

512
fig = plt.figure()
613
ax = fig.gca(projection='3d')
714
X, Y, Z = axes3d.get_test_data(0.05)
15+
16+
# Plot the 3D surface
817
ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
18+
19+
# Plot projections of the contours for each dimension. By choosing offsets
20+
# that match the appropriate axes limits, the projected contours will sit on
21+
# the 'walls' of the graph
922
cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)
1023
cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
1124
cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)
1225

13-
ax.set_xlabel('X')
1426
ax.set_xlim(-40, 40)
15-
ax.set_ylabel('Y')
1627
ax.set_ylim(-40, 40)
17-
ax.set_zlabel('Z')
1828
ax.set_zlim(-100, 100)
1929

30+
ax.set_xlabel('X')
31+
ax.set_ylabel('Y')
32+
ax.set_zlabel('Z')
33+
2034
plt.show()

‎examples/mplot3d/contourf3d_demo.py

Copy file name to clipboard
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
'''
2+
contourf differs from contour in that it creates filled contours, ie.
3+
a discrete number of colours are used to shade the domain.
4+
5+
This is like a contourf plot in 2D except that the shaded region corresponding
6+
to the level c is graphed on the plane z=c.
7+
'''
8+
19
from mpl_toolkits.mplot3d import axes3d
210
import matplotlib.pyplot as plt
311
from matplotlib import cm
412

513
fig = plt.figure()
614
ax = fig.gca(projection='3d')
715
X, Y, Z = axes3d.get_test_data(0.05)
16+
817
cset = ax.contourf(X, Y, Z, cmap=cm.coolwarm)
18+
919
ax.clabel(cset, fontsize=9, inline=1)
1020

1121
plt.show()

‎examples/mplot3d/contourf3d_demo2.py

Copy file name to clipboard
+16-7Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
"""
2-
.. versionadded:: 1.1.0
3-
This demo depends on new features added to contourf3d.
4-
"""
1+
'''
2+
Demonstrates displaying a 3D surface while also projecting filled contour
3+
'profiles' onto the 'walls' of the graph.
4+
5+
See contour3d_demo2 for the unfilled version.
6+
'''
57

68
from mpl_toolkits.mplot3d import axes3d
79
import matplotlib.pyplot as plt
@@ -10,16 +12,23 @@
1012
fig = plt.figure()
1113
ax = fig.gca(projection='3d')
1214
X, Y, Z = axes3d.get_test_data(0.05)
15+
16+
# Plot the 3D surface
1317
ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
18+
19+
# Plot projections of the contours for each dimension. By choosing offsets
20+
# that match the appropriate axes limits, the projected contours will sit on
21+
# the 'walls' of the graph
1422
cset = ax.contourf(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)
1523
cset = ax.contourf(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
1624
cset = ax.contourf(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)
1725

18-
ax.set_xlabel('X')
1926
ax.set_xlim(-40, 40)
20-
ax.set_ylabel('Y')
2127
ax.set_ylim(-40, 40)
22-
ax.set_zlabel('Z')
2328
ax.set_zlim(-100, 100)
2429

30+
ax.set_xlabel('X')
31+
ax.set_ylabel('Y')
32+
ax.set_zlabel('Z')
33+
2534
plt.show()

‎examples/mplot3d/custom_shaded_3d_surface.py

Copy file name to clipboardExpand all lines: examples/mplot3d/custom_shaded_3d_surface.py
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""
22
Demonstrates using custom hillshading in a 3D surface plot.
33
"""
4+
45
from mpl_toolkits.mplot3d import Axes3D
56
from matplotlib import cbook
67
from matplotlib import cm
78
from matplotlib.colors import LightSource
89
import matplotlib.pyplot as plt
910
import numpy as np
1011

12+
# Load and format data
1113
filename = cbook.get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
1214
with np.load(filename) as dem:
1315
z = dem['elevation']
@@ -19,6 +21,7 @@
1921
region = np.s_[5:50, 5:50]
2022
x, y, z = x[region], y[region], z[region]
2123

24+
# Set up plot
2225
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
2326

2427
ls = LightSource(270, 45)

‎examples/mplot3d/lines3d_demo.py

Copy file name to clipboardExpand all lines: examples/mplot3d/lines3d_demo.py
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
'''
2+
Demonstrating plotting a parametric curve in 3D.
3+
'''
4+
15
import matplotlib as mpl
26
from mpl_toolkits.mplot3d import Axes3D
37
import numpy as np
@@ -7,11 +11,14 @@
711

812
fig = plt.figure()
913
ax = fig.gca(projection='3d')
14+
15+
# Prepare arrays x, y, z
1016
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
1117
z = np.linspace(-2, 2, 100)
1218
r = z**2 + 1
1319
x = r * np.sin(theta)
1420
y = r * np.cos(theta)
21+
1522
ax.plot(x, y, z, label='parametric curve')
1623
ax.legend()
1724

‎examples/mplot3d/lorenz_attractor.py

Copy file name to clipboardExpand all lines: examples/mplot3d/lorenz_attractor.py
+27-15Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,54 @@
1-
# Plot of the Lorenz Attractor based on Edward Lorenz's 1963 "Deterministic
2-
# Nonperiodic Flow" publication.
3-
# http://journals.ametsoc.org/doi/abs/10.1175/1520-0469%281963%29020%3C0130%3ADNF%3E2.0.CO%3B2
4-
#
5-
# Note: Because this is a simple non-linear ODE, it would be more easily
6-
# done using SciPy's ode solver, but this approach depends only
7-
# upon NumPy.
1+
'''
2+
Plot of the Lorenz Attractor based on Edward Lorenz's 1963 "Deterministic
3+
Nonperiodic Flow" publication.
4+
http://journals.ametsoc.org/doi/abs/10.1175/1520-0469%281963%29020%3C0130%3ADNF%3E2.0.CO%3B2
5+
6+
Note: Because this is a simple non-linear ODE, it would be more easily
7+
done using SciPy's ode solver, but this approach depends only
8+
upon NumPy.
9+
'''
810

911
import numpy as np
1012
import matplotlib.pyplot as plt
1113
from mpl_toolkits.mplot3d import Axes3D
1214

1315

1416
def lorenz(x, y, z, s=10, r=28, b=2.667):
17+
'''
18+
Given:
19+
x, y, z: a point of interest in three dimensional space
20+
s, r, b: parameters defining the lorenz attractor
21+
Returns:
22+
x_dot, y_dot, z_dot: values of the lorenz attractor's partial
23+
derivatives at the point x, y, z
24+
'''
1525
x_dot = s*(y - x)
1626
y_dot = r*x - y - x*z
1727
z_dot = x*y - b*z
1828
return x_dot, y_dot, z_dot
1929

2030

2131
dt = 0.01
22-
stepCnt = 10000
32+
num_steps = 10000
2333

2434
# Need one more for the initial values
25-
xs = np.empty((stepCnt + 1,))
26-
ys = np.empty((stepCnt + 1,))
27-
zs = np.empty((stepCnt + 1,))
35+
xs = np.empty((num_steps + 1,))
36+
ys = np.empty((num_steps + 1,))
37+
zs = np.empty((num_steps + 1,))
2838

29-
# Setting initial values
39+
# Set initial values
3040
xs[0], ys[0], zs[0] = (0., 1., 1.05)
3141

32-
# Stepping through "time".
33-
for i in range(stepCnt):
34-
# Derivatives of the X, Y, Z state
42+
# Step through "time", calculating the partial derivatives at the current point
43+
# and using them to estimate the next point
44+
for i in range(num_steps):
3545
x_dot, y_dot, z_dot = lorenz(xs[i], ys[i], zs[i])
3646
xs[i + 1] = xs[i] + (x_dot * dt)
3747
ys[i + 1] = ys[i] + (y_dot * dt)
3848
zs[i + 1] = zs[i] + (z_dot * dt)
3949

50+
51+
# Plot
4052
fig = plt.figure()
4153
ax = fig.gca(projection='3d')
4254

0 commit comments

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