Skip to content

Commit c98270e

Browse files
committed
Simplify getitem.
1 parent da9e87c commit c98270e

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

lib/matplotlib/colors.py

+44-29
Original file line numberDiff line numberDiff line change
@@ -766,15 +766,18 @@ def __getitem__(self, item):
766766
767767
# ### float indexing
768768
# 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]
773777
774778
# 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]
778781
779782
# ### Int-style indexing
780783
# for int-style indexing, the values must be in [0, self.N]
@@ -788,19 +791,35 @@ def __getitem__(self, item):
788791
789792
# And so is `np.mgrid` complex-indexing (same as above)
790793
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]]
791805
"""
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):
802807
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:
804823
# This is an integer-style itemization
805824
if sss[0] is None:
806825
sss[0] = 0
@@ -816,27 +835,23 @@ def __getitem__(self, item):
816835
sss[1] = sss[1] / self.N
817836
if not isinstance(sss[2], complex):
818837
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")
828840
points = np.mgrid[slice(*sss)]
829841
elif isinstance(item, (list, np.ndarray)):
830-
name = self.name + '[...]'
842+
name = self.name + '[<indexed>]'
831843
if isinstance(item, list):
832844
item = np.array(item)
833845
if item.dtype.kind in ('u', 'i'):
834846
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")
835850
points = item
836851
else:
837852
raise IndexError("Invalid colorbar itemization")
838853
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.")
840855
return ListedColormap(self(points), name=name)
841856

842857

0 commit comments

Comments
 (0)