@@ -714,20 +714,26 @@ class Colormap:
714714 chain.
715715 """
716716
717- def __init__ (self , name , N = 256 ):
717+ def __init__ (self , name , N = 256 , * , bad = None , under = None , over = None ):
718718 """
719719 Parameters
720720 ----------
721721 name : str
722722 The name of the colormap.
723723 N : int
724724 The number of RGB quantization levels.
725+ bad : :mpltype:`color`, default: transparent
726+ The color for invalid values (NaN or masked).
727+ under : :mpltype:`color`, default: color of the lowest value
728+ The color for low out-of-range values.
729+ over : :mpltype:`color`, default: color of the highest value
730+ The color for high out-of-range values.
725731 """
726732 self .name = name
727733 self .N = int (N ) # ensure that N is always int
728- self ._rgba_bad = (0.0 , 0.0 , 0.0 , 0.0 ) # If bad, don't paint anything.
729- self ._rgba_under = None
730- self ._rgba_over = None
734+ self ._rgba_bad = (0.0 , 0.0 , 0.0 , 0.0 ) if bad is None else to_rgba ( bad )
735+ self ._rgba_under = None if under is None else to_rgba ( under )
736+ self ._rgba_over = None if over is None else to_rgba ( over )
731737 self ._i_under = self .N
732738 self ._i_over = self .N + 1
733739 self ._i_bad = self .N + 2
@@ -1034,43 +1040,61 @@ class LinearSegmentedColormap(Colormap):
10341040 segments.
10351041 """
10361042
1037- def __init__ (self , name , segmentdata , N = 256 , gamma = 1.0 ):
1043+ def __init__ (self , name , segmentdata , N = 256 , gamma = 1.0 , * ,
1044+ bad = None , under = None , over = None ):
10381045 """
1039- Create colormap from linear mapping segments
1040-
1041- segmentdata argument is a dictionary with a red, green and blue
1042- entries. Each entry should be a list of *x*, *y0*, *y1* tuples,
1043- forming rows in a table. Entries for alpha are optional.
1044-
1045- Example: suppose you want red to increase from 0 to 1 over
1046- the bottom half, green to do the same over the middle half,
1047- and blue over the top half. Then you would use::
1048-
1049- cdict = {'red': [(0.0, 0.0, 0.0),
1050- (0.5, 1.0, 1.0),
1051- (1.0, 1.0, 1.0)],
1046+ Create colormap from linear mapping segments.
10521047
1053- 'green': [(0.0, 0.0, 0.0),
1054- (0.25, 0.0, 0.0),
1055- (0.75, 1.0, 1.0),
1056- (1.0, 1.0, 1.0)],
1048+ Parameters
1049+ ----------
1050+ name : str
1051+ The name of the colormap.
1052+ segmentdata : dict
1053+ A dictionary with keys "red", "green", "blue" for the color channels.
1054+ Each entry should be a list of *x*, *y0*, *y1* tuples, forming rows
1055+ in a table. Entries for alpha are optional.
1056+
1057+ Example: suppose you want red to increase from 0 to 1 over
1058+ the bottom half, green to do the same over the middle half,
1059+ and blue over the top half. Then you would use::
1060+
1061+ {
1062+ 'red': [(0.0, 0.0, 0.0),
1063+ (0.5, 1.0, 1.0),
1064+ (1.0, 1.0, 1.0)],
1065+ 'green': [(0.0, 0.0, 0.0),
1066+ (0.25, 0.0, 0.0),
1067+ (0.75, 1.0, 1.0),
1068+ (1.0, 1.0, 1.0)],
1069+ 'blue': [(0.0, 0.0, 0.0),
1070+ (0.5, 0.0, 0.0),
1071+ (1.0, 1.0, 1.0)]
1072+ }
10571073
1058- 'blue': [(0.0, 0.0, 0.0),
1059- (0.5, 0.0, 0.0),
1060- (1.0, 1.0, 1.0)]}
1074+ Each row in the table for a given color is a sequence of
1075+ *x*, *y0*, *y1* tuples. In each sequence, *x* must increase
1076+ monotonically from 0 to 1. For any input value *z* falling
1077+ between *x[i]* and *x[i+1]*, the output value of a given color
1078+ will be linearly interpolated between *y1[i]* and *y0[i+1]*::
10611079
1062- Each row in the table for a given color is a sequence of
1063- *x*, *y0*, *y1* tuples. In each sequence, *x* must increase
1064- monotonically from 0 to 1. For any input value *z* falling
1065- between *x[i]* and *x[i+1]*, the output value of a given color
1066- will be linearly interpolated between *y1[i]* and *y0[i+1]*::
1080+ row i: x y0 y1
1081+ /
1082+ /
1083+ row i+1: x y0 y1
10671084
1068- row i: x y0 y1
1069- /
1070- /
1071- row i+1: x y0 y1
1085+ Hence, y0 in the first row and y1 in the last row are never used.
10721086
1073- Hence y0 in the first row and y1 in the last row are never used.
1087+ N : int
1088+ The number of RGB quantization levels.
1089+ gamma : float
1090+ Gamma correction factor for input distribution x of the mapping.
1091+ See also https://en.wikipedia.org/wiki/Gamma_correction.
1092+ bad : :mpltype:`color`, default: transparent
1093+ The color for invalid values (NaN or masked).
1094+ under : :mpltype:`color`, default: color of the lowest value
1095+ The color for low out-of-range values.
1096+ over : :mpltype:`color`, default: color of the highest value
1097+ The color for high out-of-range values.
10741098
10751099 See Also
10761100 --------
@@ -1080,7 +1104,7 @@ def __init__(self, name, segmentdata, N=256, gamma=1.0):
10801104 """
10811105 # True only if all colors in map are identical; needed for contouring.
10821106 self .monochrome = False
1083- super ().__init__ (name , N )
1107+ super ().__init__ (name , N , bad = bad , under = under , over = over )
10841108 self ._segmentdata = segmentdata
10851109 self ._gamma = gamma
10861110
@@ -1104,7 +1128,7 @@ def set_gamma(self, gamma):
11041128 self ._init ()
11051129
11061130 @staticmethod
1107- def from_list (name , colors , N = 256 , gamma = 1.0 ):
1131+ def from_list (name , colors , N = 256 , gamma = 1.0 , * , bad = None , under = None , over = None ):
11081132 """
11091133 Create a `LinearSegmentedColormap` from a list of colors.
11101134
@@ -1121,6 +1145,13 @@ def from_list(name, colors, N=256, gamma=1.0):
11211145 N : int
11221146 The number of RGB quantization levels.
11231147 gamma : float
1148+
1149+ bad : :mpltype:`color`, default: transparent
1150+ The color for invalid values (NaN or masked).
1151+ under : :mpltype:`color`, default: color of the lowest value
1152+ The color for low out-of-range values.
1153+ over : :mpltype:`color`, default: color of the highest value
1154+ The color for high out-of-range values.
11241155 """
11251156 if not np .iterable (colors ):
11261157 raise ValueError ('colors must be iterable' )
@@ -1140,7 +1171,8 @@ def from_list(name, colors, N=256, gamma=1.0):
11401171 "alpha" : np .column_stack ([vals , a , a ]),
11411172 }
11421173
1143- return LinearSegmentedColormap (name , cdict , N , gamma )
1174+ return LinearSegmentedColormap (name , cdict , N , gamma ,
1175+ bad = bad , under = under , over = over )
11441176
11451177 def resampled (self , lutsize ):
11461178 """Return a new colormap with *lutsize* entries."""
@@ -1215,6 +1247,18 @@ class ListedColormap(Colormap):
12151247 N > len(colors)
12161248
12171249 the list will be extended by repetition.
1250+
1251+ .. deprecated:: 3.11
1252+
1253+ This parameter will be removed. Please ensure instead that
1254+ the list of passed colors is the required length.
1255+
1256+ bad : :mpltype:`color`, default: transparent
1257+ The color for invalid values (NaN or masked).
1258+ under : :mpltype:`color`, default: color of the lowest value
1259+ The color for low out-of-range values.
1260+ over : :mpltype:`color`, default: color of the highest value
1261+ The color for high out-of-range values.
12181262 """
12191263
12201264 @_api .delete_parameter (
@@ -1223,7 +1267,8 @@ class ListedColormap(Colormap):
12231267 "and will be removed in %(removal)s. Please ensure the list "
12241268 "of passed colors is the required length instead."
12251269 )
1226- def __init__ (self , colors , name = 'from_list' , N = None ):
1270+ def __init__ (self , colors , name = 'from_list' , N = None , * ,
1271+ bad = None , under = None , over = None ):
12271272 if N is None :
12281273 self .colors = colors
12291274 N = len (colors )
@@ -1240,7 +1285,7 @@ def __init__(self, colors, name='from_list', N=None):
12401285 pass
12411286 else :
12421287 self .colors = [gray ] * N
1243- super ().__init__ (name , N )
1288+ super ().__init__ (name , N , bad = bad , under = under , over = over )
12441289
12451290 def _init (self ):
12461291 self ._lut = np .zeros ((self .N + 3 , 4 ), float )
@@ -3744,8 +3789,7 @@ def from_levels_and_colors(levels, colors, extend='neither'):
37443789 data_colors = colors [color_slice ]
37453790 under_color = colors [0 ] if extend in ['min' , 'both' ] else 'none'
37463791 over_color = colors [- 1 ] if extend in ['max' , 'both' ] else 'none'
3747- cmap = ListedColormap (data_colors ).with_extremes (
3748- under = under_color , over = over_color )
3792+ cmap = ListedColormap (data_colors , under = under_color , over = over_color )
37493793
37503794 cmap .colorbar_extend = extend
37513795
0 commit comments