@@ -766,15 +766,18 @@ def __getitem__(self, item):
766
766
767
767
# ### float indexing
768
768
# for float-style indexing, the values must be in [0.0, 1.0]
769
- # Truncate the colormap between 20 and 70%
770
- new_cm = cmap[0.2, 0.7]
771
- # equivalently:
772
- new_cm = cmap[0.2:0.7]
769
+ # Truncate the colormap between 20 and 80%.
770
+ new_cm = cmap[0.2:0.6]
771
+ # `new_cm` will have the color-spacing as `cmap` (in this
772
+ # case: 0.6 - 0.2 = 40% of 128 = 51 colors)
773
+
774
+ # negative values are supported
775
+ # this gives the same result as above
776
+ new_cm = cmap[0.2:-0.4]
773
777
774
778
# Same as above, but specify the number of points
775
- new_cm = cmap[0.2, 0.7, 64]
776
- # equivalently, use `np.mgrid` complex-indexing:
777
- new_cm = cmap[0.2:0.7:1j * 64]
779
+ # using `np.mgrid` complex-indexing:
780
+ new_cm = cmap[0.2:-0.4:1j * 64]
778
781
779
782
# ### Int-style indexing
780
783
# for int-style indexing, the values must be in [0, self.N]
@@ -788,19 +791,35 @@ def __getitem__(self, item):
788
791
789
792
# And so is `np.mgrid` complex-indexing (same as above)
790
793
new_cm = cmap[12:-28:1j * 22]
794
+
795
+ # ### Array/list-style indexing
796
+ # In this case, you specify specific points in the colormap
797
+ # at which you'd like to create a new colormap.
798
+
799
+ # You can index by integers, in which case
800
+ # all values must be ints in [-self.N, self.N]:
801
+ new_cm = cmap[[5, 10, 25, -38]]
802
+
803
+ # Or by floats in the range [-1, 1]
804
+ new_cm = cmap[[0.04, 0.08, 0.2, -0.3]]
791
805
"""
792
- if isinstance (item , tuple ):
793
- if len (item ) == 2 :
794
- N = self .N
795
- elif len (item ) == 3 :
796
- N = item [2 ]
797
- else :
798
- raise IndexError ("Invalid colorbar itemization" )
799
- return self .truncate (item [0 ], item [1 ], N = N )
800
- elif isinstance (item , slice ):
801
- name = self .name + '[{:s}]' .format (str (item ))
806
+ if isinstance (item , slice ):
802
807
sss = [item .start , item .stop , item .step ]
803
- if any ([isinstance (s , int ) for s in sss ]):
808
+ name = self .name + '[{}:{}:{}]' .format (* sss )
809
+ if (all ([s is None or abs (s ) <= 1 for s in sss [:2 ]]) and
810
+ (sss [2 ] is None or abs (sss [2 ]) <= 1 or
811
+ isinstance (sss [2 ], complex ))):
812
+ if sss [0 ] is None :
813
+ sss [0 ] = 0
814
+ elif sss [0 ] < 0 :
815
+ sss [0 ] += 1
816
+ if sss [1 ] is None :
817
+ sss [1 ] = 1.0
818
+ elif sss [1 ] < 0 :
819
+ sss [1 ] += 1
820
+ if sss [2 ] is None :
821
+ sss [2 ] = self .N * 1j * (sss [1 ] - sss [0 ])
822
+ else :
804
823
# This is an integer-style itemization
805
824
if sss [0 ] is None :
806
825
sss [0 ] = 0
@@ -816,27 +835,23 @@ def __getitem__(self, item):
816
835
sss [1 ] = sss [1 ] / self .N
817
836
if not isinstance (sss [2 ], complex ):
818
837
sss [2 ] = sss [2 ] / self .N
819
- else :
820
- if sss [0 ] is None :
821
- sss [0 ] = 0.0
822
- if sss [1 ] is None :
823
- sss [1 ] = 1.0
824
- if sss [2 ] is None :
825
- sss [2 ] = self .N * 1j
826
- if sss [0 ] < 0 or sss [0 ] >= 1 or sss [1 ] <= 0 or sss [1 ] > 1 :
827
- raise IndexError ("Invalid colorbar itemization - outside bounds" )
838
+ if sss [0 ] < 0 or sss [0 ] >= 1 or sss [1 ] <= 0 or sss [1 ] > 1 :
839
+ raise IndexError ("Invalid colorbar itemization - outside bounds" )
828
840
points = np .mgrid [slice (* sss )]
829
841
elif isinstance (item , (list , np .ndarray )):
830
- name = self .name + '[... ]'
842
+ name = self .name + '[<indexed> ]'
831
843
if isinstance (item , list ):
832
844
item = np .array (item )
833
845
if item .dtype .kind in ('u' , 'i' ):
834
846
item = item .astype ('f' ) / self .N
847
+ item [item < 0 ] += 1
848
+ if np .any (item > 1 ):
849
+ raise IndexError ("Invalid colorbar itemization - outside bounds" )
835
850
points = item
836
851
else :
837
852
raise IndexError ("Invalid colorbar itemization" )
838
853
if len (points ) <= 1 :
839
- raise IndexError ("Invalid colorbar itemization - too few points " )
854
+ raise IndexError ("Invalid colorbar itemization - a colorbar must contain >1 color. " )
840
855
return ListedColormap (self (points ), name = name )
841
856
842
857
0 commit comments