@@ -714,20 +714,26 @@ class Colormap:
714
714
chain.
715
715
"""
716
716
717
- def __init__ (self , name , N = 256 ):
717
+ def __init__ (self , name , N = 256 , * , bad = None , under = None , over = None ):
718
718
"""
719
719
Parameters
720
720
----------
721
721
name : str
722
722
The name of the colormap.
723
723
N : int
724
724
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.
725
731
"""
726
732
self .name = name
727
733
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 )
731
737
self ._i_under = self .N
732
738
self ._i_over = self .N + 1
733
739
self ._i_bad = self .N + 2
@@ -1034,43 +1040,61 @@ class LinearSegmentedColormap(Colormap):
1034
1040
segments.
1035
1041
"""
1036
1042
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 ):
1038
1045
"""
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.
1052
1047
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
+ }
1057
1073
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]*::
1061
1079
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
1067
1084
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.
1072
1086
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.
1074
1098
1075
1099
See Also
1076
1100
--------
@@ -1080,7 +1104,7 @@ def __init__(self, name, segmentdata, N=256, gamma=1.0):
1080
1104
"""
1081
1105
# True only if all colors in map are identical; needed for contouring.
1082
1106
self .monochrome = False
1083
- super ().__init__ (name , N )
1107
+ super ().__init__ (name , N , bad = bad , under = under , over = over )
1084
1108
self ._segmentdata = segmentdata
1085
1109
self ._gamma = gamma
1086
1110
@@ -1104,7 +1128,7 @@ def set_gamma(self, gamma):
1104
1128
self ._init ()
1105
1129
1106
1130
@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 ):
1108
1132
"""
1109
1133
Create a `LinearSegmentedColormap` from a list of colors.
1110
1134
@@ -1121,6 +1145,13 @@ def from_list(name, colors, N=256, gamma=1.0):
1121
1145
N : int
1122
1146
The number of RGB quantization levels.
1123
1147
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.
1124
1155
"""
1125
1156
if not np .iterable (colors ):
1126
1157
raise ValueError ('colors must be iterable' )
@@ -1140,7 +1171,8 @@ def from_list(name, colors, N=256, gamma=1.0):
1140
1171
"alpha" : np .column_stack ([vals , a , a ]),
1141
1172
}
1142
1173
1143
- return LinearSegmentedColormap (name , cdict , N , gamma )
1174
+ return LinearSegmentedColormap (name , cdict , N , gamma ,
1175
+ bad = bad , under = under , over = over )
1144
1176
1145
1177
def resampled (self , lutsize ):
1146
1178
"""Return a new colormap with *lutsize* entries."""
@@ -1215,6 +1247,18 @@ class ListedColormap(Colormap):
1215
1247
N > len(colors)
1216
1248
1217
1249
the list will be extended by repetition.
1250
+
1251
+ .. deprecated:: 3.11
1252
+
1253
+ This parameter will be removed. Please instead ensure 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.
1218
1262
"""
1219
1263
1220
1264
@_api .delete_parameter (
@@ -1223,7 +1267,8 @@ class ListedColormap(Colormap):
1223
1267
"and will be removed in %(removal)s. Please ensure the list "
1224
1268
"of passed colors is the required length instead."
1225
1269
)
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 ):
1227
1272
if N is None :
1228
1273
self .colors = colors
1229
1274
N = len (colors )
@@ -1240,7 +1285,7 @@ def __init__(self, colors, name='from_list', N=None):
1240
1285
pass
1241
1286
else :
1242
1287
self .colors = [gray ] * N
1243
- super ().__init__ (name , N )
1288
+ super ().__init__ (name , N , bad = bad , under = under , over = over )
1244
1289
1245
1290
def _init (self ):
1246
1291
self ._lut = np .zeros ((self .N + 3 , 4 ), float )
@@ -3744,8 +3789,7 @@ def from_levels_and_colors(levels, colors, extend='neither'):
3744
3789
data_colors = colors [color_slice ]
3745
3790
under_color = colors [0 ] if extend in ['min' , 'both' ] else 'none'
3746
3791
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 )
3749
3793
3750
3794
cmap .colorbar_extend = extend
3751
3795
0 commit comments