@@ -718,20 +718,34 @@ class Colormap:
718
718
chain.
719
719
"""
720
720
721
- def __init__ (self , name , N = 256 ):
721
+ def __init__ (self , name , N = 256 , * , bad = None , under = None , over = None ):
722
722
"""
723
723
Parameters
724
724
----------
725
725
name : str
726
726
The name of the colormap.
727
727
N : int
728
728
The number of RGB quantization levels.
729
+ bad : :mpltype:`color`, default: transparent
730
+ The color for invalid values (NaN or masked).
731
+
732
+ .. versionadded:: 3.11
733
+
734
+ under : :mpltype:`color`, default: color of the lowest value
735
+ The color for low out-of-range values.
736
+
737
+ .. versionadded:: 3.11
738
+
739
+ over : :mpltype:`color`, default: color of the highest value
740
+ The color for high out-of-range values.
741
+
742
+ .. versionadded:: 3.11
729
743
"""
730
744
self .name = name
731
745
self .N = int (N ) # ensure that N is always int
732
- self ._rgba_bad = (0.0 , 0.0 , 0.0 , 0.0 ) # If bad, don't paint anything.
733
- self ._rgba_under = None
734
- self ._rgba_over = None
746
+ self ._rgba_bad = (0.0 , 0.0 , 0.0 , 0.0 ) if bad is None else to_rgba ( bad )
747
+ self ._rgba_under = None if under is None else to_rgba ( under )
748
+ self ._rgba_over = None if over is None else to_rgba ( over )
735
749
self ._i_under = self .N
736
750
self ._i_over = self .N + 1
737
751
self ._i_bad = self .N + 2
@@ -1038,43 +1052,69 @@ class LinearSegmentedColormap(Colormap):
1038
1052
segments.
1039
1053
"""
1040
1054
1041
- def __init__ (self , name , segmentdata , N = 256 , gamma = 1.0 ):
1055
+ def __init__ (self , name , segmentdata , N = 256 , gamma = 1.0 , * ,
1056
+ bad = None , under = None , over = None ):
1042
1057
"""
1043
- Create colormap from linear mapping segments
1058
+ Create colormap from linear mapping segments.
1044
1059
1045
- segmentdata argument is a dictionary with a red, green and blue
1046
- entries. Each entry should be a list of *x*, *y0*, *y1* tuples,
1047
- forming rows in a table. Entries for alpha are optional.
1060
+ Parameters
1061
+ ----------
1062
+ name : str
1063
+ The name of the colormap.
1064
+ segmentdata : dict
1065
+ A dictionary with keys "red", "green", "blue" for the color channels.
1066
+ Each entry should be a list of *x*, *y0*, *y1* tuples, forming rows
1067
+ in a table. Entries for alpha are optional.
1068
+
1069
+ Example: suppose you want red to increase from 0 to 1 over
1070
+ the bottom half, green to do the same over the middle half,
1071
+ and blue over the top half. Then you would use::
1072
+
1073
+ {
1074
+ 'red': [(0.0, 0.0, 0.0),
1075
+ (0.5, 1.0, 1.0),
1076
+ (1.0, 1.0, 1.0)],
1077
+ 'green': [(0.0, 0.0, 0.0),
1078
+ (0.25, 0.0, 0.0),
1079
+ (0.75, 1.0, 1.0),
1080
+ (1.0, 1.0, 1.0)],
1081
+ 'blue': [(0.0, 0.0, 0.0),
1082
+ (0.5, 0.0, 0.0),
1083
+ (1.0, 1.0, 1.0)]
1084
+ }
1048
1085
1049
- Example: suppose you want red to increase from 0 to 1 over
1050
- the bottom half, green to do the same over the middle half,
1051
- and blue over the top half. Then you would use::
1086
+ Each row in the table for a given color is a sequence of
1087
+ *x*, *y0*, *y1* tuples. In each sequence, *x* must increase
1088
+ monotonically from 0 to 1. For any input value *z* falling
1089
+ between *x[i]* and *x[i+1]*, the output value of a given color
1090
+ will be linearly interpolated between *y1[i]* and *y0[i+1]*::
1052
1091
1053
- cdict = {'red': [(0.0, 0.0, 0.0),
1054
- (0.5, 1.0, 1.0),
1055
- (1.0, 1.0, 1.0)],
1092
+ row i: x y0 y1
1093
+ /
1094
+ /
1095
+ row i+1: x y0 y1
1056
1096
1057
- 'green': [(0.0, 0.0, 0.0),
1058
- (0.25, 0.0, 0.0),
1059
- (0.75, 1.0, 1.0),
1060
- (1.0, 1.0, 1.0)],
1097
+ Hence, y0 in the first row and y1 in the last row are never used.
1061
1098
1062
- 'blue': [(0.0, 0.0, 0.0),
1063
- (0.5, 0.0, 0.0),
1064
- (1.0, 1.0, 1.0)]}
1099
+ N : int
1100
+ The number of RGB quantization levels.
1101
+ gamma : float
1102
+ Gamma correction factor for input distribution x of the mapping.
1103
+ See also https://en.wikipedia.org/wiki/Gamma_correction.
1104
+ bad : :mpltype:`color`, default: transparent
1105
+ The color for invalid values (NaN or masked).
1106
+
1107
+ .. versionadded:: 3.11
1108
+
1109
+ under : :mpltype:`color`, default: color of the lowest value
1110
+ The color for low out-of-range values.
1065
1111
1066
- Each row in the table for a given color is a sequence of
1067
- *x*, *y0*, *y1* tuples. In each sequence, *x* must increase
1068
- monotonically from 0 to 1. For any input value *z* falling
1069
- between *x[i]* and *x[i+1]*, the output value of a given color
1070
- will be linearly interpolated between *y1[i]* and *y0[i+1]*::
1112
+ .. versionadded:: 3.11
1071
1113
1072
- row i: x y0 y1
1073
- /
1074
- /
1075
- row i+1: x y0 y1
1114
+ over : :mpltype:`color`, default: color of the highest value
1115
+ The color for high out-of-range values.
1076
1116
1077
- Hence y0 in the first row and y1 in the last row are never used.
1117
+ .. versionadded:: 3.11
1078
1118
1079
1119
See Also
1080
1120
--------
@@ -1084,7 +1124,7 @@ def __init__(self, name, segmentdata, N=256, gamma=1.0):
1084
1124
"""
1085
1125
# True only if all colors in map are identical; needed for contouring.
1086
1126
self .monochrome = False
1087
- super ().__init__ (name , N )
1127
+ super ().__init__ (name , N , bad = bad , under = under , over = over )
1088
1128
self ._segmentdata = segmentdata
1089
1129
self ._gamma = gamma
1090
1130
@@ -1108,7 +1148,7 @@ def set_gamma(self, gamma):
1108
1148
self ._init ()
1109
1149
1110
1150
@staticmethod
1111
- def from_list (name , colors , N = 256 , gamma = 1.0 ):
1151
+ def from_list (name , colors , N = 256 , gamma = 1.0 , * , bad = None , under = None , over = None ):
1112
1152
"""
1113
1153
Create a `LinearSegmentedColormap` from a list of colors.
1114
1154
@@ -1125,6 +1165,13 @@ def from_list(name, colors, N=256, gamma=1.0):
1125
1165
N : int
1126
1166
The number of RGB quantization levels.
1127
1167
gamma : float
1168
+
1169
+ bad : :mpltype:`color`, default: transparent
1170
+ The color for invalid values (NaN or masked).
1171
+ under : :mpltype:`color`, default: color of the lowest value
1172
+ The color for low out-of-range values.
1173
+ over : :mpltype:`color`, default: color of the highest value
1174
+ The color for high out-of-range values.
1128
1175
"""
1129
1176
if not np .iterable (colors ):
1130
1177
raise ValueError ('colors must be iterable' )
@@ -1144,7 +1191,8 @@ def from_list(name, colors, N=256, gamma=1.0):
1144
1191
"alpha" : np .column_stack ([vals , a , a ]),
1145
1192
}
1146
1193
1147
- return LinearSegmentedColormap (name , cdict , N , gamma )
1194
+ return LinearSegmentedColormap (name , cdict , N , gamma ,
1195
+ bad = bad , under = under , over = over )
1148
1196
1149
1197
def resampled (self , lutsize ):
1150
1198
"""Return a new colormap with *lutsize* entries."""
@@ -1219,6 +1267,26 @@ class ListedColormap(Colormap):
1219
1267
N > len(colors)
1220
1268
1221
1269
the list will be extended by repetition.
1270
+
1271
+ .. deprecated:: 3.11
1272
+
1273
+ This parameter will be removed. Please instead ensure that
1274
+ the list of passed colors is the required length.
1275
+
1276
+ bad : :mpltype:`color`, default: transparent
1277
+ The color for invalid values (NaN or masked).
1278
+
1279
+ .. versionadded:: 3.11
1280
+
1281
+ under : :mpltype:`color`, default: color of the lowest value
1282
+ The color for low out-of-range values.
1283
+
1284
+ .. versionadded:: 3.11
1285
+
1286
+ over : :mpltype:`color`, default: color of the highest value
1287
+ The color for high out-of-range values.
1288
+
1289
+ .. versionadded:: 3.11
1222
1290
"""
1223
1291
1224
1292
@_api .delete_parameter (
@@ -1227,7 +1295,8 @@ class ListedColormap(Colormap):
1227
1295
"and will be removed in %(removal)s. Please ensure the list "
1228
1296
"of passed colors is the required length instead."
1229
1297
)
1230
- def __init__ (self , colors , name = 'from_list' , N = None ):
1298
+ def __init__ (self , colors , name = 'from_list' , N = None , * ,
1299
+ bad = None , under = None , over = None ):
1231
1300
if N is None :
1232
1301
self .colors = colors
1233
1302
N = len (colors )
@@ -1244,7 +1313,7 @@ def __init__(self, colors, name='from_list', N=None):
1244
1313
pass
1245
1314
else :
1246
1315
self .colors = [gray ] * N
1247
- super ().__init__ (name , N )
1316
+ super ().__init__ (name , N , bad = bad , under = under , over = over )
1248
1317
1249
1318
def _init (self ):
1250
1319
self ._lut = np .zeros ((self .N + 3 , 4 ), float )
@@ -3748,8 +3817,7 @@ def from_levels_and_colors(levels, colors, extend='neither'):
3748
3817
data_colors = colors [color_slice ]
3749
3818
under_color = colors [0 ] if extend in ['min' , 'both' ] else 'none'
3750
3819
over_color = colors [- 1 ] if extend in ['max' , 'both' ] else 'none'
3751
- cmap = ListedColormap (data_colors ).with_extremes (
3752
- under = under_color , over = over_color )
3820
+ cmap = ListedColormap (data_colors , under = under_color , over = over_color )
3753
3821
3754
3822
cmap .colorbar_extend = extend
3755
3823
0 commit comments