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

Skip to content

Commit 18851b2

Browse files
committed
Add gallery examples
1 parent b6a703b commit 18851b2

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

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, color=colors, edgecolor='k')
34+
35+
plt.show()

examples/mplot3d/voxels_rgb.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
# prepare some coordinates, and attach rgb values to each
14+
x, y, z = np.indices((16, 16, 16))
15+
r = (x + 0.5) / 16
16+
g = (y + 0.5) / 16
17+
b = (z + 0.5) / 16
18+
19+
# define a sphere about [0.5, 0.5, 0.5]
20+
sphere = (r - 0.5)**2 + (g - 0.5)**2 + (b - 0.5)**2 < 0.5**2
21+
22+
# combine the color components
23+
colors = np.zeros(sphere.shape + (3,))
24+
colors[..., 0] = r
25+
colors[..., 1] = g
26+
colors[..., 2] = b
27+
28+
# and plot everything
29+
fig = plt.figure()
30+
ax = fig.gca(projection='3d')
31+
ax.voxels(sphere, color=colors, edgecolor='gray', linewidth=0.5)
32+
33+
plt.show()

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2785,6 +2785,12 @@ def voxels(self, filled, color=None, **kwargs):
27852785
``filled[i,j,k]``. If no faces were drawn for a given voxel, either
27862786
because it was not asked to be drawn, or it is fully occluded, then
27872787
``(i,j,k) not in faces``.
2788+
2789+
Examples
2790+
--------
2791+
2792+
.. plot:: gallery/mplot3d/voxels.py
2793+
.. plot:: gallery/mplot3d/voxels_rgb.py
27882794
"""
27892795
# check dimensions
27902796
if filled.ndim != 3:

0 commit comments

Comments
 (0)