@@ -665,6 +665,25 @@ def _resample(self, lutsize):
665665 """
666666 raise NotImplementedError ()
667667
668+ def reversed (self , name = None ):
669+ """
670+ Make a reversed instance of the Colormap.
671+
672+ .. note :: Function not implemented for base class.
673+
674+ Parameters
675+ ----------
676+ name : str, optional
677+ The name for the reversed colormap. If it's None the
678+ name will be the name of the parent colormap + "_r".
679+
680+ Notes
681+ -----
682+ See :meth:`LinearSegmentedColormap.reversed` and
683+ :meth:`ListedColormap.reversed`
684+ """
685+ raise NotImplementedError ()
686+
668687
669688class LinearSegmentedColormap (Colormap ):
670689 """Colormap objects based on lookup tables using linear segments.
@@ -784,6 +803,40 @@ def _resample(self, lutsize):
784803 """
785804 return LinearSegmentedColormap (self .name , self ._segmentdata , lutsize )
786805
806+ def reversed (self , name = None ):
807+ """
808+ Make a reversed instance of the Colormap.
809+
810+ Parameters
811+ ----------
812+ name : str, optional
813+ The name for the reversed colormap. If it's None the
814+ name will be the name of the parent colormap + "_r".
815+
816+ Returns
817+ -------
818+ LinearSegmentedColormap
819+ The reversed colormap.
820+ """
821+ if name is None :
822+ name = self .name + "_r"
823+
824+ # Function factory needed to deal with 'late binding' issue.
825+ def factory (dat ):
826+ def func_r (x ):
827+ return dat (1.0 - x )
828+ return func_r
829+
830+ data_r = dict ()
831+ for key , data in six .iteritems (self ._segmentdata ):
832+ if six .callable (data ):
833+ data_r [key ] = factory (data )
834+ else :
835+ new_data = [(1.0 - x , y1 , y0 ) for x , y0 , y1 in reversed (data )]
836+ data_r [key ] = new_data
837+
838+ return LinearSegmentedColormap (name , data_r , self .N , self ._gamma )
839+
787840
788841class ListedColormap (Colormap ):
789842 """Colormap object generated from a list of colors.
@@ -856,6 +909,27 @@ def _resample(self, lutsize):
856909 colors = self (np .linspace (0 , 1 , lutsize ))
857910 return ListedColormap (colors , name = self .name )
858911
912+ def reversed (self , name = None ):
913+ """
914+ Make a reversed instance of the Colormap.
915+
916+ Parameters
917+ ----------
918+ name : str, optional
919+ The name for the reversed colormap. If it's None the
920+ name will be the name of the parent colormap + "_r".
921+
922+ Returns
923+ -------
924+ ListedColormap
925+ A reversed instance of the colormap.
926+ """
927+ if name is None :
928+ name = self .name + "_r"
929+
930+ colors_r = list (reversed (self .colors ))
931+ return ListedColormap (colors_r , name = name , N = self .N )
932+
859933
860934class Normalize (object ):
861935 """
0 commit comments