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

Skip to content

Commit 0612ed5

Browse files
authored
Merge pull request #6690 from TrishGillett/mplot3d-examples-MEP12-b
Tidying up and tweaking mplot3d examples [MEP12]
2 parents b5d28f3 + d47dcce commit 0612ed5

17 files changed

+320
-97
lines changed

examples/mplot3d/2dcollections3d_demo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828

2929
# Make legend, set axes limits and labels
3030
ax.legend()
31-
ax.set_xlim3d(0, 1)
32-
ax.set_ylim3d(0, 1)
33-
ax.set_zlim3d(0, 1)
31+
ax.set_xlim(0, 1)
32+
ax.set_ylim(0, 1)
33+
ax.set_zlim(0, 1)
3434
ax.set_xlabel('X')
3535
ax.set_ylabel('Y')
3636
ax.set_zlabel('Z')

examples/mplot3d/quiver3d_demo.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1+
'''
2+
==============
3+
3D quiver plot
4+
==============
5+
6+
Demonstrates plotting directional arrows at points on a 3d meshgrid.
7+
'''
8+
19
from mpl_toolkits.mplot3d import axes3d
210
import matplotlib.pyplot as plt
311
import numpy as np
412

513
fig = plt.figure()
614
ax = fig.gca(projection='3d')
715

16+
# Make the grid
817
x, y, z = np.meshgrid(np.arange(-0.8, 1, 0.2),
918
np.arange(-0.8, 1, 0.2),
1019
np.arange(-0.8, 1, 0.8))
1120

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

614
fig = plt.figure()
715
ax = fig.add_subplot(111, projection='3d')
16+
17+
# load some test data for demonstration and plot a wireframe
818
X, Y, Z = axes3d.get_test_data(0.1)
919
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)
1020

21+
# rotate the axes and update
1122
for angle in range(0, 360):
1223
ax.view_init(30, angle)
1324
plt.draw()
25+
plt.pause(.001)

examples/mplot3d/scatter3d_demo.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
1-
import numpy as np
1+
'''
2+
==============
3+
3D scatterplot
4+
==============
5+
6+
Demonstration of a basic scatterplot in 3D.
7+
'''
8+
29
from mpl_toolkits.mplot3d import Axes3D
310
import matplotlib.pyplot as plt
11+
import numpy as np
412

513

614
def randrange(n, vmin, vmax):
15+
'''
16+
Helper function to make an array of random numbers having shape (n, )
17+
with each number distributed Uniform(vmin, vmax).
18+
'''
719
return (vmax - vmin)*np.random.rand(n) + vmin
820

921
fig = plt.figure()
1022
ax = fig.add_subplot(111, projection='3d')
23+
1124
n = 100
12-
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
25+
26+
# For each set of style and range settings, plot n random points in the box
27+
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
28+
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
1329
xs = randrange(n, 23, 32)
1430
ys = randrange(n, 0, 100)
15-
zs = randrange(n, zl, zh)
31+
zs = randrange(n, zlow, zhigh)
1632
ax.scatter(xs, ys, zs, c=c, marker=m)
1733

1834
ax.set_xlabel('X Label')

examples/mplot3d/subplot3d_demo.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
1-
from mpl_toolkits.mplot3d.axes3d import Axes3D
2-
import matplotlib.pyplot as plt
1+
'''
2+
====================
3+
3D plots as subplots
4+
====================
35
6+
Demonstrate including 3D plots as subplots.
7+
'''
48

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

10-
# Twice as wide as it is tall.
14+
15+
# set up a figure twice as wide as it is tall
1116
fig = plt.figure(figsize=plt.figaspect(0.5))
1217

13-
#---- First subplot
18+
#===============
19+
# First subplot
20+
#===============
21+
# set up the axes for the first plot
1422
ax = fig.add_subplot(1, 2, 1, projection='3d')
23+
24+
# plot a 3D surface like in the example mplot3d/surface3d_demo
1525
X = np.arange(-5, 5, 0.25)
1626
Y = np.arange(-5, 5, 0.25)
1727
X, Y = np.meshgrid(X, Y)
1828
R = np.sqrt(X**2 + Y**2)
1929
Z = np.sin(R)
2030
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
2131
linewidth=0, antialiased=False)
22-
ax.set_zlim3d(-1.01, 1.01)
23-
32+
ax.set_zlim(-1.01, 1.01)
2433
fig.colorbar(surf, shrink=0.5, aspect=10)
2534

26-
#---- Second subplot
35+
#===============
36+
# Second subplot
37+
#===============
38+
# set up the axes for the second plot
2739
ax = fig.add_subplot(1, 2, 2, projection='3d')
40+
41+
# plot a 3D wireframe like in the example mplot3d/wire3d_demo
2842
X, Y, Z = get_test_data(0.05)
2943
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
3044

examples/mplot3d/surface3d_demo.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
1+
'''
2+
======================
3+
3D surface (color map)
4+
======================
5+
6+
Demonstrates plotting a 3D surface colored with the coolwarm color map.
7+
The surface is made opaque by using antialiased=False.
8+
9+
Also demonstrates using the LinearLocator and custom formatting for the
10+
z axis tick labels.
11+
'''
12+
113
from mpl_toolkits.mplot3d import Axes3D
14+
import matplotlib.pyplot as plt
215
from matplotlib import cm
316
from matplotlib.ticker import LinearLocator, FormatStrFormatter
4-
import matplotlib.pyplot as plt
517
import numpy as np
618

19+
720
fig = plt.figure()
821
ax = fig.gca(projection='3d')
22+
23+
# Make data.
924
X = np.arange(-5, 5, 0.25)
1025
Y = np.arange(-5, 5, 0.25)
1126
X, Y = np.meshgrid(X, Y)
1227
R = np.sqrt(X**2 + Y**2)
1328
Z = np.sin(R)
29+
30+
# Plot the surface.
1431
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
1532
linewidth=0, antialiased=False)
16-
ax.set_zlim(-1.01, 1.01)
1733

34+
# Customize the z axis.
35+
ax.set_zlim(-1.01, 1.01)
1836
ax.zaxis.set_major_locator(LinearLocator(10))
1937
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
2038

39+
# Add a color bar which maps values to colors.
2140
fig.colorbar(surf, shrink=0.5, aspect=5)
2241

2342
plt.show()

examples/mplot3d/surface3d_demo2.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1+
'''
2+
========================
3+
3D surface (solid color)
4+
========================
5+
6+
Demonstrates a very basic plot of a 3D surface using a solid color.
7+
'''
8+
19
from mpl_toolkits.mplot3d import Axes3D
210
import matplotlib.pyplot as plt
311
import numpy as np
412

13+
514
fig = plt.figure()
615
ax = fig.add_subplot(111, projection='3d')
716

17+
# Make data
818
u = np.linspace(0, 2 * np.pi, 100)
919
v = np.linspace(0, np.pi, 100)
10-
1120
x = 10 * np.outer(np.cos(u), np.sin(v))
1221
y = 10 * np.outer(np.sin(u), np.sin(v))
1322
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
23+
24+
# Plot the surface
1425
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
1526

1627
plt.show()

examples/mplot3d/surface3d_demo3.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1+
'''
2+
=========================
3+
3D surface (checkerboard)
4+
=========================
5+
6+
Demonstrates plotting a 3D surface colored in a checkerboard pattern.
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
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
xlen = len(X)
1122
Y = np.arange(-5, 5, 0.25)
@@ -14,16 +25,20 @@
1425
R = np.sqrt(X**2 + Y**2)
1526
Z = np.sin(R)
1627

28+
# Create an empty array of strings with the same shape as the meshgrid, and
29+
# populate it with two colors in a checkerboard pattern.
1730
colortuple = ('y', 'b')
1831
colors = np.empty(X.shape, dtype=str)
1932
for y in range(ylen):
2033
for x in range(xlen):
2134
colors[x, y] = colortuple[(x + y) % len(colortuple)]
2235

36+
# Plot the surface with face colors taken from the array we made.
2337
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors,
2438
linewidth=0)
2539

26-
ax.set_zlim3d(-1, 1)
40+
# Customize the z axis.
41+
ax.set_zlim(-1, 1)
2742
ax.w_zaxis.set_major_locator(LinearLocator(6))
2843

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

313
from mpl_toolkits.mplot3d import Axes3D
4-
import matplotlib
5-
import numpy as np
6-
from matplotlib import cm
714
from matplotlib import pyplot as plt
8-
step = 0.04
9-
maxval = 1.0
15+
import numpy as np
16+
17+
1018
fig = plt.figure()
1119
ax = fig.add_subplot(111, projection='3d')
1220

13-
# create supporting points in polar coordinates
21+
# Create the mesh in polar coordinates and compute corresponding Z.
1422
r = np.linspace(0, 1.25, 50)
1523
p = np.linspace(0, 2*np.pi, 50)
1624
R, P = np.meshgrid(r, p)
17-
# transform them to cartesian system
25+
Z = ((R**2 - 1)**2)
26+
27+
# Express the mesh in the cartesian system.
1828
X, Y = R*np.cos(P), R*np.sin(P)
1929

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)
30+
# Plot the surface.
31+
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.YlGnBu_r)
32+
33+
# Tweak the limits and add latex math labels.
34+
ax.set_zlim(0, 1)
2335
ax.set_xlabel(r'$\phi_\mathrm{real}$')
2436
ax.set_ylabel(r'$\phi_\mathrm{im}$')
2537
ax.set_zlabel(r'$V(\phi)$')
38+
2639
plt.show()

examples/mplot3d/text3d_demo.py

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

18+
419
fig = plt.figure()
520
ax = fig.gca(projection='3d')
621

22+
# Demo 1: zdir
723
zdirs = (None, 'x', 'y', 'z', (1, 1, 0), (1, 1, 1))
824
xs = (1, 4, 4, 9, 4, 1)
925
ys = (2, 5, 8, 10, 1, 2)
@@ -13,13 +29,17 @@
1329
label = '(%d, %d, %d), dir=%s' % (x, y, z, zdir)
1430
ax.text(x, y, z, label, zdir)
1531

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

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

39+
# Tweaking display region and labels
40+
ax.set_xlim(0, 10)
41+
ax.set_ylim(0, 10)
42+
ax.set_zlim(0, 10)
2343
ax.set_xlabel('X axis')
2444
ax.set_ylabel('Y axis')
2545
ax.set_zlabel('Z axis')

0 commit comments

Comments
 (0)