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

Skip to content

Commit df50224

Browse files
committed
Improve documentation for the color argument
1 parent 1e73931 commit df50224

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from matplotlib.tri.triangulation import Triangulation
3232
from matplotlib import colors as mcolors
3333
from matplotlib.colors import Normalize, LightSource
34+
from matplotlib.cbook._backports import broadcast_to
3435

3536
from . import art3d
3637
from . import proj3d
@@ -2762,9 +2763,16 @@ def voxels(self, filled, color=None, **kwargs):
27622763
to fill
27632764
27642765
color : array_like
2765-
Either a single value or an array the same shape as filled,
2766-
indicating what color to draw the faces of the voxels. If None,
2767-
plot all voxels in the same color, the next in the color sequence.
2766+
The color to draw the faces of the voxels. This parameter can be:
2767+
2768+
- A single color value, to color all voxels the same color. This
2769+
can be either a string, or a 1D rgb/rgba array
2770+
- ``None``, indicating the above with the next color in the
2771+
sequence
2772+
- A 3D ndarray of color names, with each item the color for the
2773+
corresponding voxel. The size must match the voxels.
2774+
- A 4D ndarray of rgb/rgba data, with the components along the
2775+
last axis.
27682776
27692777
Any additional keyword arguments are passed onto
27702778
:func:`~mpl_toolkits.mplot3d.art3d.Poly3DCollection`
@@ -2785,15 +2793,18 @@ def voxels(self, filled, color=None, **kwargs):
27852793
# handle the color argument
27862794
if color is None:
27872795
color = self._get_patches_for_fill.get_next_color()
2788-
if np.ndim(color) <= 1:
2789-
color, _ = np.broadcast_arrays(
2790-
color,
2791-
filled[np.index_exp[...] + np.index_exp[np.newaxis] * np.ndim(color)]
2792-
)
2793-
elif np.ndim(color) < 3:
2794-
raise ValueError("Argument color must be at least 3-dimensional")
2795-
elif np.shape(color)[:3] != filled.shape:
2796-
raise ValueError("Argument color must match the shape of filled, if multidimensional")
2796+
if np.ndim(color) in (0, 1):
2797+
# single color, like "red" or [1, 0, 0]
2798+
color = broadcast_to(color, filled.shape + np.shape(color))
2799+
elif np.ndim(color) in (3, 4):
2800+
# 3D array of strings, or 4D array with last axis rgb
2801+
if np.shape(color)[:3] != filled.shape:
2802+
raise ValueError(
2803+
"When multidimensional, color must match the shape of "
2804+
"filled")
2805+
else:
2806+
raise ValueError("Invalid color argument")
2807+
27972808

27982809
# always scale to the full array, even if the data is only in the center
27992810
self.auto_scale_xyz(

0 commit comments

Comments
 (0)