Skip to content

Commit 52a1f12

Browse files
committed
DOC: Add 3D plots to plot_types gallery
1 parent 4965500 commit 52a1f12

File tree

7 files changed

+191
-0
lines changed

7 files changed

+191
-0
lines changed

doc/sphinxext/gallery_order.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
'../plot_types/arrays',
2929
'../plot_types/stats',
3030
'../plot_types/unstructured',
31+
'../plot_types/3D',
3132
]
3233

3334

plot_types/3D/README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.. _3D_plots:
2+
3+
3D
4+
--
5+
6+
3D plot types.

plot_types/3D/scatter3d.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
==============
3+
3D scatterplot
4+
==============
5+
6+
Demonstration of a basic scatterplot in 3D.
7+
"""
8+
9+
import matplotlib.pyplot as plt
10+
import numpy as np
11+
12+
# Fixing random state for reproducibility
13+
np.random.seed(19680801)
14+
15+
16+
def randrange(n, vmin, vmax):
17+
"""
18+
Helper function to make an array of random numbers having shape (n, )
19+
with each number distributed Uniform(vmin, vmax).
20+
"""
21+
return (vmax - vmin)*np.random.rand(n) + vmin
22+
23+
fig = plt.figure()
24+
ax = fig.add_subplot(projection='3d')
25+
26+
n = 100
27+
28+
# For each set of style and range settings, plot n random points in the box
29+
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
30+
for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
31+
xs = randrange(n, 23, 32)
32+
ys = randrange(n, 0, 100)
33+
zs = randrange(n, zlow, zhigh)
34+
ax.scatter(xs, ys, zs, marker=m)
35+
36+
ax.set_xlabel('X Label')
37+
ax.set_ylabel('Y Label')
38+
ax.set_zlabel('Z Label')
39+
40+
plt.show()

plot_types/3D/surface3d.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
=====================
3+
3D surface (colormap)
4+
=====================
5+
6+
Demonstrates plotting a 3D surface colored with the coolwarm colormap.
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+
13+
import matplotlib.pyplot as plt
14+
from matplotlib import cm
15+
from matplotlib.ticker import LinearLocator
16+
import numpy as np
17+
18+
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
19+
20+
# Make data.
21+
X = np.arange(-5, 5, 0.25)
22+
Y = np.arange(-5, 5, 0.25)
23+
X, Y = np.meshgrid(X, Y)
24+
R = np.sqrt(X**2 + Y**2)
25+
Z = np.sin(R)
26+
27+
# Plot the surface.
28+
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
29+
linewidth=0, antialiased=False)
30+
31+
# Customize the z axis.
32+
ax.set_zlim(-1.01, 1.01)
33+
ax.zaxis.set_major_locator(LinearLocator(10))
34+
# A StrMethodFormatter is used automatically
35+
ax.zaxis.set_major_formatter('{x:.02f}')
36+
37+
# Add a color bar which maps values to colors.
38+
fig.colorbar(surf, shrink=0.5, aspect=5)
39+
40+
plt.show()
41+
42+
43+
#############################################################################
44+
#
45+
# .. admonition:: References
46+
#
47+
# The use of the following functions, methods, classes and modules is shown
48+
# in this example:
49+
#
50+
# - `matplotlib.pyplot.subplots`
51+
# - `matplotlib.axis.Axis.set_major_formatter`
52+
# - `matplotlib.axis.Axis.set_major_locator`
53+
# - `matplotlib.ticker.LinearLocator`
54+
# - `matplotlib.ticker.StrMethodFormatter`

plot_types/3D/trisurf3d.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
======================
3+
Triangular 3D surfaces
4+
======================
5+
6+
Plot a 3D surface with a triangular mesh.
7+
"""
8+
9+
import matplotlib.pyplot as plt
10+
import numpy as np
11+
12+
13+
n_radii = 8
14+
n_angles = 36
15+
16+
# Make radii and angles spaces (radius r=0 omitted to eliminate duplication).
17+
radii = np.linspace(0.125, 1.0, n_radii)
18+
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)[..., np.newaxis]
19+
20+
# Convert polar (radii, angles) coords to cartesian (x, y) coords.
21+
# (0, 0) is manually added at this stage, so there will be no duplicate
22+
# points in the (x, y) plane.
23+
x = np.append(0, (radii*np.cos(angles)).flatten())
24+
y = np.append(0, (radii*np.sin(angles)).flatten())
25+
26+
# Compute z to make the pringle surface.
27+
z = np.sin(-x*y)
28+
29+
ax = plt.figure().add_subplot(projection='3d')
30+
31+
ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)
32+
33+
plt.show()

plot_types/3D/voxels.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
==========================
3+
3D voxel / volumetric plot
4+
==========================
5+
6+
Demonstrates plotting 3D volumetric objects with `.Axes3D.voxels`.
7+
"""
8+
9+
import matplotlib.pyplot as plt
10+
import numpy as np
11+
12+
13+
# prepare some coordinates
14+
x, y, z = np.indices((8, 8, 8))
15+
16+
# draw cuboids in the top left and bottom right corners, and a link between
17+
# them
18+
cube1 = (x < 3) & (y < 3) & (z < 3)
19+
cube2 = (x >= 5) & (y >= 5) & (z >= 5)
20+
link = abs(x - y) + abs(y - z) + abs(z - x) <= 2
21+
22+
# combine the objects into a single boolean array
23+
voxelarray = cube1 | cube2 | link
24+
25+
# set the colors of each object
26+
colors = np.empty(voxelarray.shape, dtype=object)
27+
colors[link] = 'red'
28+
colors[cube1] = 'blue'
29+
colors[cube2] = 'green'
30+
31+
# and plot everything
32+
ax = plt.figure().add_subplot(projection='3d')
33+
ax.voxels(voxelarray, facecolors=colors, edgecolor='k')
34+
35+
plt.show()

plot_types/3D/wire3d.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
=================
3+
3D wireframe plot
4+
=================
5+
6+
A very basic demonstration of a wireframe plot.
7+
"""
8+
9+
from mpl_toolkits.mplot3d import axes3d
10+
import matplotlib.pyplot as plt
11+
12+
13+
fig = plt.figure()
14+
ax = fig.add_subplot(projection='3d')
15+
16+
# Grab some test data.
17+
X, Y, Z = axes3d.get_test_data(0.05)
18+
19+
# Plot a basic wireframe.
20+
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
21+
22+
plt.show()

0 commit comments

Comments
 (0)