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

Skip to content

Commit a8ddde9

Browse files
authored
Merge pull request matplotlib#6404 from eric-wieser/patch-1
ENH: Add a ax.voxels(bool3d) function
2 parents 926efd4 + 4003858 commit a8ddde9

File tree

13 files changed

+537
-0
lines changed

13 files changed

+537
-0
lines changed

doc/users/whats_new/axes3d_voxels.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``voxels`` function for mplot3d
2+
-------------------------------
3+
:class:`~mpl_toolkits.mplot3d.axes3d.Axes3D` now has a ``voxels`` method, for
4+
visualizing boolean 3d data. Uses could include plotting a sparse 3D heat map,
5+
or visualizing a volumetric model.

examples/mplot3d/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 ``ax.voxels``
7+
'''
8+
9+
import matplotlib.pyplot as plt
10+
import numpy as np
11+
from mpl_toolkits.mplot3d import Axes3D
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 them
17+
cube1 = (x < 3) & (y < 3) & (z < 3)
18+
cube2 = (x >= 5) & (y >= 5) & (z >= 5)
19+
link = abs(x - y) + abs(y - z) + abs(z - x) <= 2
20+
21+
# combine the objects into a single boolean array
22+
voxels = cube1 | cube2 | link
23+
24+
# set the colors of each object
25+
colors = np.empty(voxels.shape, dtype=object)
26+
colors[link] = 'red'
27+
colors[cube1] = 'blue'
28+
colors[cube2] = 'green'
29+
30+
# and plot everything
31+
fig = plt.figure()
32+
ax = fig.gca(projection='3d')
33+
ax.voxels(voxels, facecolors=colors, edgecolor='k')
34+
35+
plt.show()

examples/mplot3d/voxels_numpy_logo.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
===============================
3+
3D voxel plot of the numpy logo
4+
===============================
5+
6+
Demonstrates using ``ax.voxels`` with uneven coordinates
7+
'''
8+
import matplotlib.pyplot as plt
9+
import numpy as np
10+
from mpl_toolkits.mplot3d import Axes3D
11+
12+
13+
def explode(data):
14+
size = np.array(data.shape)*2
15+
data_e = np.zeros(size - 1, dtype=data.dtype)
16+
data_e[::2, ::2, ::2] = data
17+
return data_e
18+
19+
# build up the numpy logo
20+
n_voxels = np.zeros((4, 3, 4), dtype=bool)
21+
n_voxels[0, 0, :] = True
22+
n_voxels[-1, 0, :] = True
23+
n_voxels[1, 0, 2] = True
24+
n_voxels[2, 0, 1] = True
25+
facecolors = np.where(n_voxels, '#FFD65DC0', '#7A88CCC0')
26+
edgecolors = np.where(n_voxels, '#BFAB6E', '#7D84A6')
27+
filled = np.ones(n_voxels.shape)
28+
29+
# upscale the above voxel image, leaving gaps
30+
filled_2 = explode(filled)
31+
fcolors_2 = explode(facecolors)
32+
ecolors_2 = explode(edgecolors)
33+
34+
# Shrink the gaps
35+
x, y, z = np.indices(np.array(filled_2.shape) + 1).astype(float) // 2
36+
x[0::2, :, :] += 0.05
37+
y[:, 0::2, :] += 0.05
38+
z[:, :, 0::2] += 0.05
39+
x[1::2, :, :] += 0.95
40+
y[:, 1::2, :] += 0.95
41+
z[:, :, 1::2] += 0.95
42+
43+
fig = plt.figure()
44+
ax = fig.gca(projection='3d')
45+
ax.voxels(x, y, z, filled_2, facecolors=fcolors_2, edgecolors=ecolors_2)
46+
47+
plt.show()

examples/mplot3d/voxels_rgb.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
==========================================
3+
3D voxel / volumetric plot with rgb colors
4+
==========================================
5+
6+
Demonstrates using ``ax.voxels`` to visualize parts of a color space
7+
'''
8+
9+
import matplotlib.pyplot as plt
10+
import numpy as np
11+
from mpl_toolkits.mplot3d import Axes3D
12+
13+
14+
def midpoints(x):
15+
sl = ()
16+
for i in range(x.ndim):
17+
x = (x[sl + np.index_exp[:-1]] + x[sl + np.index_exp[1:]]) / 2.0
18+
sl += np.index_exp[:]
19+
return x
20+
21+
# prepare some coordinates, and attach rgb values to each
22+
r, g, b = np.indices((17, 17, 17)) / 16.0
23+
rc = midpoints(r)
24+
gc = midpoints(g)
25+
bc = midpoints(b)
26+
27+
# define a sphere about [0.5, 0.5, 0.5]
28+
sphere = (rc - 0.5)**2 + (gc - 0.5)**2 + (bc - 0.5)**2 < 0.5**2
29+
30+
# combine the color components
31+
colors = np.zeros(sphere.shape + (3,))
32+
colors[..., 0] = rc
33+
colors[..., 1] = gc
34+
colors[..., 2] = bc
35+
36+
# and plot everything
37+
fig = plt.figure()
38+
ax = fig.gca(projection='3d')
39+
ax.voxels(r, g, b, sphere,
40+
facecolors=colors,
41+
edgecolors=np.clip(2*colors - 0.5, 0, 1), # brighter
42+
linewidth=0.5)
43+
ax.set(xlabel='r', ylabel='g', zlabel='b')
44+
45+
plt.show()

examples/mplot3d/voxels_torus.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
=======================================================
3+
3D voxel / volumetric plot with cylindrical coordinates
4+
=======================================================
5+
6+
Demonstrates using the ``x, y, z`` arguments of ``ax.voxels``.
7+
'''
8+
9+
import matplotlib.pyplot as plt
10+
import matplotlib.colors
11+
import numpy as np
12+
from mpl_toolkits.mplot3d import Axes3D
13+
14+
15+
def midpoints(x):
16+
sl = ()
17+
for i in range(x.ndim):
18+
x = (x[sl + np.index_exp[:-1]] + x[sl + np.index_exp[1:]]) / 2.0
19+
sl += np.index_exp[:]
20+
return x
21+
22+
# prepare some coordinates, and attach rgb values to each
23+
r, theta, z = np.mgrid[0:1:11j, 0:np.pi*2:25j, -0.5:0.5:11j]
24+
x = r*np.cos(theta)
25+
y = r*np.sin(theta)
26+
27+
rc, thetac, zc = midpoints(r), midpoints(theta), midpoints(z)
28+
29+
# define a wobbly torus about [0.7, *, 0]
30+
sphere = (rc - 0.7)**2 + (zc + 0.2*np.cos(thetac*2))**2 < 0.2**2
31+
32+
# combine the color components
33+
hsv = np.zeros(sphere.shape + (3,))
34+
hsv[..., 0] = thetac / (np.pi*2)
35+
hsv[..., 1] = rc
36+
hsv[..., 2] = zc + 0.5
37+
colors = matplotlib.colors.hsv_to_rgb(hsv)
38+
39+
# and plot everything
40+
fig = plt.figure()
41+
ax = fig.gca(projection='3d')
42+
ax.voxels(x, y, z, sphere,
43+
facecolors=colors,
44+
edgecolors=np.clip(2*colors - 0.5, 0, 1), # brighter
45+
linewidth=0.5)
46+
47+
plt.show()

0 commit comments

Comments
 (0)