@@ -752,51 +752,66 @@ def __init__(self, name, N=256, *, bad=None, under=None, over=None):
752
752
#: `matplotlib.colorbar.Colorbar` constructor.
753
753
self .colorbar_extend = False
754
754
755
- def __call__ (self , X , alpha = None , bytes = False ):
755
+ def __call__ (self , X , alpha = None , bytes = False , by_index = 'auto' ):
756
756
r"""
757
757
Parameters
758
758
----------
759
759
X : float or int or array-like
760
- The data value(s) to convert to RGBA.
761
- For floats, *X* should be in the interval ``[0.0, 1.0]`` to
762
- return the RGBA values ``X*100`` percent along the Colormap line.
763
- For integers, *X* should be in the interval ``[0, Colormap.N)`` to
764
- return RGBA values *indexed* from the Colormap with index ``X``.
760
+ The data value(s) to convert to RGBA. The interpretation (normalized
761
+ values or index values) depends on *by_index*.
765
762
alpha : float or array-like or None
766
763
Alpha must be a scalar between 0 and 1, a sequence of such
767
764
floats with shape matching X, or None.
768
765
bytes : bool, default: False
769
766
If False (default), the returned RGBA values will be floats in the
770
767
interval ``[0, 1]`` otherwise they will be `numpy.uint8`\s in the
771
768
interval ``[0, 255]``.
769
+ by_index: bool or 'auto', default: 'auto'
770
+ How the input *X* is interpreted:
771
+
772
+ - If True, *X* is treated as an array of indices into the Colormap
773
+ lookup table, i.e. the range ``[0, Colormap.N)`` covers the colormap.
774
+ - If False, *X* is treated normalized values, i.e. the range
775
+ ``[0.0, 1.0]`` covers the colormap.
776
+ - If 'auto', the type of *X* is used to determine the interpretation.
777
+ float inputs are treated like ``by_index=False``, integer inputs
778
+ are treated like ``by_index=True``.
772
779
773
780
Returns
774
781
-------
775
782
Tuple of RGBA values if X is scalar, otherwise an array of
776
783
RGBA values with a shape of ``X.shape + (4, )``.
777
784
"""
778
- rgba , mask = self ._get_rgba_and_mask (X , alpha = alpha , bytes = bytes )
785
+ rgba , mask = self ._get_rgba_and_mask (X , alpha = alpha , bytes = bytes ,
786
+ by_index = by_index )
779
787
if not np .iterable (X ):
780
788
rgba = tuple (rgba )
781
789
return rgba
782
790
783
- def _get_rgba_and_mask (self , X , alpha = None , bytes = False ):
791
+ def _get_rgba_and_mask (self , X , alpha = None , bytes = False , by_index = 'auto' ):
784
792
r"""
785
793
Parameters
786
794
----------
787
795
X : float or int or array-like
788
- The data value(s) to convert to RGBA.
789
- For floats, *X* should be in the interval ``[0.0, 1.0]`` to
790
- return the RGBA values ``X*100`` percent along the Colormap line.
791
- For integers, *X* should be in the interval ``[0, Colormap.N)`` to
792
- return RGBA values *indexed* from the Colormap with index ``X``.
796
+ The data value(s) to convert to RGBA. The interpretation (normalized
797
+ values or index values) depends on *by_index*.
793
798
alpha : float or array-like or None
794
799
Alpha must be a scalar between 0 and 1, a sequence of such
795
800
floats with shape matching X, or None.
796
801
bytes : bool, default: False
797
802
If False (default), the returned RGBA values will be floats in the
798
803
interval ``[0, 1]`` otherwise they will be `numpy.uint8`\s in the
799
804
interval ``[0, 255]``.
805
+ by_index: bool or 'auto', default: 'auto'
806
+ How the input *X* is interpreted:
807
+
808
+ - If True, *X* is treated as an array of indices into the Colormap
809
+ lookup table, i.e. the range ``[0, Colormap.N)`` covers the colormap.
810
+ - If False, *X* is treated normalized values, i.e. the range
811
+ ``[0.0, 1.0]`` covers the colormap.
812
+ - If 'auto', the type of *X* is used to determine the interpretation.
813
+ float inputs are treated like ``by_index=False``, integer inputs
814
+ are treated like ``by_index=True``.
800
815
801
816
Returns
802
817
-------
@@ -812,7 +827,7 @@ def _get_rgba_and_mask(self, X, alpha=None, bytes=False):
812
827
if not xa .dtype .isnative :
813
828
# Native byteorder is faster.
814
829
xa = xa .byteswap ().view (xa .dtype .newbyteorder ())
815
- if xa .dtype .kind == "f" :
830
+ if by_index is False or ( by_index == 'auto' and xa .dtype .kind == "f" ) :
816
831
xa *= self .N
817
832
# xa == 1 (== N after multiplication) is not out of range.
818
833
xa [xa == self .N ] = self .N - 1
0 commit comments