@@ -728,14 +728,16 @@ def __call__(self, X, alpha=None, bytes=False):
728728 bytes : bool
729729 If False (default), the returned RGBA values will be floats in the
730730 interval ``[0, 1]`` otherwise they will be `numpy.uint8`\s in the
731- interval ``[0, 255]``
731+ interval ``[0, 255]``.
732732
733733 Returns
734734 -------
735735 Tuple of RGBA values if X is scalar, otherwise an array of
736736 RGBA values with a shape of ``X.shape + (4, )``.
737737 """
738738 rgba , mask = self ._get_rgba_and_mask (X , alpha = alpha , bytes = bytes )
739+ if not np .iterable (X ):
740+ rgba = tuple (rgba )
739741 return rgba
740742
741743 def _get_rgba_and_mask (self , X , alpha = None , bytes = False ):
@@ -758,9 +760,9 @@ def _get_rgba_and_mask(self, X, alpha=None, bytes=False):
758760
759761 Returns
760762 -------
761- ( colors, mask), where color is a tuple of RGBA values if X is scalar,
762- otherwise an array of RGBA values with a shape of ``X.shape + (4, )``,
763- and mask is a boolean array .
763+ colors : array of RGBA values with a shape of ``X.shape + (4, )``.
764+ mask : boolean array with True where the input is ``np.nan`` or
765+ masked .
764766 """
765767 if not self ._isinit :
766768 self ._init ()
@@ -805,8 +807,6 @@ def _get_rgba_and_mask(self, X, alpha=None, bytes=False):
805807 if (lut [- 1 ] == 0 ).all ():
806808 rgba [mask_bad ] = (0 , 0 , 0 , 0 )
807809
808- if not np .iterable (X ):
809- rgba = tuple (rgba )
810810 return rgba , mask_bad
811811
812812 def __copy__ (self ):
@@ -1290,9 +1290,7 @@ def __init__(self, colormaps, combination_mode, name='multivariate colormap'):
12901290 " Colormap or valid strings." )
12911291
12921292 self ._colormaps = colormaps
1293- if combination_mode not in ['sRGB_add' , 'sRGB_sub' ]:
1294- raise ValueError ("Combination_mode must be 'sRGB_add' or 'sRGB_sub',"
1295- f" { combination_mode !r} is not allowed." )
1293+ _api .check_in_list (['sRGB_add' , 'sRGB_sub' ], combination_mode = combination_mode )
12961294 self ._combination_mode = combination_mode
12971295 self .n_variates = len (colormaps )
12981296 self ._rgba_bad = (0.0 , 0.0 , 0.0 , 0.0 ) # If bad, don't paint anything.
@@ -1331,10 +1329,8 @@ def __call__(self, X, alpha=None, bytes=False, clip=True):
13311329 f'For the selected colormap the data must have a first dimension '
13321330 f'{ len (self )} , not { len (X )} ' )
13331331 rgba , mask_bad = self [0 ]._get_rgba_and_mask (X [0 ], bytes = False )
1334- rgba = np .asarray (rgba )
13351332 for c , xx in zip (self [1 :], X [1 :]):
13361333 sub_rgba , sub_mask_bad = c ._get_rgba_and_mask (xx , bytes = False )
1337- sub_rgba = np .asarray (sub_rgba )
13381334 rgba [..., :3 ] += sub_rgba [..., :3 ] # add colors
13391335 rgba [..., 3 ] *= sub_rgba [..., 3 ] # multiply alpha
13401336 mask_bad |= sub_mask_bad
@@ -1418,11 +1414,10 @@ def resampled(self, lutshape):
14181414
14191415 Parameters
14201416 ----------
1421- lutshape : tuple of ints or None
1422- The tuple must be of length matching the number of variates,
1423- and each entry is either an int or None.
1424- If an int, the corresponding colorbar is resampled.
1425- If None, the corresponding colorbar is not resampled.
1417+ lutshape : tuple of (`int`, `None`)
1418+ The tuple must have a length matching the number of variates.
1419+ For each element in the tuple, if `int`, the corresponding colorbar
1420+ is resampled, if `None`, the corresponding colorbar is not resampled.
14261421
14271422 Returns
14281423 -------
@@ -1439,22 +1434,24 @@ def resampled(self, lutshape):
14391434
14401435 def with_extremes (self , * , bad = None , under = None , over = None ):
14411436 """
1442- Return a copy of the MultivarColormap, for which the colors for masked (*bad*)
1443- values has been set and, low (*under*) and high (*over*) out-of-range values,
1444- been set in the component colormaps. Note that *under* and *over* colors
1445- are subject to the mixing rules determined by the *combination_mode*.
1437+ Return a copy of the `MultivarColormap` with modified out-of-range attributes.
1438+
1439+ The *bad* keyword modifies the copied `MultivarColormap` while *under* and
1440+ *over* modifies the attributes of the copied component colormaps.
1441+ Note that *under* and *over* colors are subject to the mixing rules determined
1442+ by the *combination_mode*.
14461443
14471444 Parameters
14481445 ----------
14491446 bad : None or :mpltype:`color`
14501447 If Matplotlib color, the bad value is set accordingly in the copy
14511448
1452- under : None or tuple of length matching the length of the MultivarColormap
1449+ under : None or tuple of :mpltype:`color`
14531450 If tuple, the `under` value of each component is set with the values
14541451 from the tuple.
14551452
1456- over : None or tuple of length matching the length of the MultivarColormap
1457- If tuple, the `under ` value of each component is set with the values
1453+ over : None or tuple of :mpltype:`color`
1454+ If tuple, the `over ` value of each component is set with the values
14581455 from the tuple.
14591456
14601457 Returns
@@ -1527,7 +1524,6 @@ def __init__(self, N=256, M=256, shape='square', origin=(0, 0),
15271524 The number of RGB quantization levels along the first axis.
15281525 M : int
15291526 The number of RGB quantization levels along the second axis.
1530- If None, M = N
15311527 shape: {'square', 'circle', 'ignore', 'circleignore'}
15321528
15331529 - 'square' each variate is clipped to [0,1] independently
@@ -1550,11 +1546,8 @@ def __init__(self, N=256, M=256, shape='square', origin=(0, 0),
15501546 self .name = name
15511547 self .N = int (N ) # ensure that N is always int
15521548 self .M = int (M )
1553- if shape in ['square' , 'circle' , 'ignore' , 'circleignore' ]:
1554- self ._shape = shape
1555- else :
1556- raise ValueError ("The shape must be a valid string, "
1557- "'square', 'circle', 'ignore', or 'circleignore'" )
1549+ _api .check_in_list (['square' , 'circle' , 'ignore' , 'circleignore' ], shape = shape )
1550+ self ._shape = shape
15581551 self ._rgba_bad = (0.0 , 0.0 , 0.0 , 0.0 ) # If bad, don't paint anything.
15591552 self ._rgba_outside = (1.0 , 0.0 , 1.0 , 1.0 )
15601553 self ._isinit = False
@@ -1737,8 +1730,8 @@ def resampled(self, lutshape, transposed=False):
17371730 The tuple must be of length 2, and each entry is either an int or None.
17381731
17391732 - If an int, the corresponding axis is resampled.
1740- - If -1, the axis is inverted
17411733 - If negative the corresponding axis is resampled in reverse
1734+ - If -1, the axis is inverted
17421735 - If 1 or None, the corresponding axis is not resampled.
17431736
17441737 transposed : bool
@@ -1814,9 +1807,10 @@ def transposed(self):
18141807
18151808 def with_extremes (self , * , bad = None , outside = None , shape = None , origin = None ):
18161809 """
1817- Return a copy of the BivarColormap, for which the colors for masked (*bad*)
1818- valuesand if shape = 'ignore' or 'circleignore', out-of-range *outside* values,
1819- have been set accordingly.
1810+ Return a copy of the `BivarColormap` with modified attributes.
1811+
1812+ Note that the *outside* color is only relevantif `shape` = 'ignore'
1813+ or 'circleignore'.
18201814
18211815 Parameters
18221816 ----------
@@ -1854,11 +1848,9 @@ def with_extremes(self, *, bad=None, outside=None, shape=None, origin=None):
18541848 if outside is not None :
18551849 new_cm ._rgba_outside = to_rgba (outside )
18561850 if shape is not None :
1857- if shape in ['square' , 'circle' , 'ignore' , 'circleignore' ]:
1858- new_cm ._shape = shape
1859- else :
1860- raise ValueError ("The shape must be a valid string, "
1861- "'square', 'circle', 'ignore', or 'circleignore'" )
1851+ _api .check_in_list (['square' , 'circle' , 'ignore' , 'circleignore' ],
1852+ shape = shape )
1853+ new_cm ._shape = shape
18621854 if origin is not None :
18631855 new_cm ._origin = (float (origin [0 ]), float (origin [1 ]))
18641856
@@ -2013,7 +2005,7 @@ def color_block(color):
20132005 '</div>'
20142006 '<div style="float: right;">'
20152007 f'bad { color_block (self .get_bad ())} '
2016- '</div>' )
2008+ '</div></div> ' )
20172009
20182010 def copy (self ):
20192011 """Return a copy of the colormap."""
@@ -2051,6 +2043,7 @@ class SegmentedBivarColormap(BivarColormap):
20512043
20522044 def __init__ (self , patch , N = 256 , shape = 'square' , origin = (0 , 0 ),
20532045 name = 'segmented bivariate colormap' ):
2046+ _api .check_shape ((None , None , 3 ), patch = patch )
20542047 self .patch = patch
20552048 super ().__init__ (N , N , shape , origin , name = name )
20562049
0 commit comments