26
26
from matplotlib ._cm_listed import cmaps as cmaps_listed
27
27
28
28
29
- _LUTSIZE = mpl .rcParams [" image.lut" ]
29
+ _LUTSIZE = mpl .rcParams [' image.lut' ]
30
30
31
31
32
32
def _gen_cmap_registry ():
@@ -38,22 +38,20 @@ def _gen_cmap_registry():
38
38
for name , spec in datad .items ():
39
39
cmap_d [name ] = ( # Precache the cmaps at a fixed lutsize..
40
40
colors .LinearSegmentedColormap (name , spec , _LUTSIZE )
41
- if "red" in spec
42
- else colors .ListedColormap (spec ["listed" ], name )
43
- if "listed" in spec
44
- else colors .LinearSegmentedColormap .from_list (name , spec , _LUTSIZE )
45
- )
41
+ if 'red' in spec else
42
+ colors .ListedColormap (spec ['listed' ], name )
43
+ if 'listed' in spec else
44
+ colors .LinearSegmentedColormap .from_list (name , spec , _LUTSIZE ))
46
45
47
- # Register colormap aliases for gray and grey.
46
+ # Register colormap aliases for gray and grey.
48
47
cmap_d ["grey" ] = cmap_d ["gray" ]
49
- cmap_d ["Greys" ] = cmap_d ["Grays" ]
50
48
cmap_d ["gist_grey" ] = cmap_d ["gist_gray" ]
49
+ cmap_d ["Grays" ] = cmap_d<
10000
/span>["Greys" ]
51
50
52
51
# Generate reversed cmaps.
53
52
for cmap in list (cmap_d .values ()):
54
53
rmap = cmap .reversed ()
55
54
cmap_d [rmap .name ] = rmap
56
-
57
55
return cmap_d
58
56
59
57
@@ -76,7 +74,6 @@ class ColormapRegistry(Mapping):
76
74
77
75
mpl.colormaps.register(my_colormap)
78
76
"""
79
-
80
77
def __init__ (self , cmaps ):
81
78
self ._cmaps = cmaps
82
79
self ._builtin_cmaps = tuple (cmaps )
@@ -96,9 +93,8 @@ def __len__(self):
96
93
return len (self ._cmaps )
97
94
98
95
def __str__ (self ):
99
- return "ColormapRegistry; available colormaps:\n " + ", " .join (
100
- f"'{ name } '" for name in self
101
- )
96
+ return ('ColormapRegistry; available colormaps:\n ' +
97
+ ', ' .join (f"'{ name } '" for name in self ))
102
98
103
99
def __call__ (self ):
104
100
"""
@@ -142,20 +138,25 @@ def register(self, cmap, *, name=None, force=False):
142
138
if not force :
143
139
# don't allow registering an already existing cmap
144
140
# unless explicitly asked to
145
- raise ValueError (f'A colormap named "{ name } " is already registered.' )
146
- elif name in self ._builtin_cmaps and not self ._allow_override_builtin :
141
+ raise ValueError (
142
+ f'A colormap named "{ name } " is already registered.' )
143
+ elif (name in self ._builtin_cmaps
144
+ and not self ._allow_override_builtin ):
147
145
# We don't allow overriding a builtin unless privately
148
146
# coming from register_cmap()
149
- raise ValueError (
150
- "Re-registering the builtin cmap " f"{ name !r} is not allowed."
151
- )
147
+ raise ValueError ("Re-registering the builtin cmap "
148
+ f"{ name !r} is not allowed." )
152
149
153
150
# Warn that we are updating an already existing colormap
154
- _api .warn_external (
155
- f"Overwriting the cmap { name !r} " "that was already in the registry."
156
- )
151
+ _api .warn_external (f"Overwriting the cmap { name !r} "
152
+ "that was already in the registry." )
157
153
158
154
self ._cmaps [name ] = cmap .copy ()
155
+ # Someone may set the extremes of a builtin colormap and want to register it
156
+ # with a different name for future lookups. The object would still have the
157
+ # builtin name, so we should update it to the registered name
158
+ if self ._cmaps [name ].name != name :
159
+ self ._cmaps [name ].name = name
159
160
160
161
def unregister (self , name ):
161
162
"""
@@ -185,9 +186,8 @@ def unregister(self, name):
185
186
If you try to remove a default built-in colormap.
186
187
"""
187
188
if name in self ._builtin_cmaps :
188
- raise ValueError (
189
- f"cannot unregister { name !r} which is a builtin " "colormap."
190
- )
189
+ raise ValueError (f"cannot unregister { name !r} which is a builtin "
190
+ "colormap." )
191
191
self ._cmaps .pop (name , None )
192
192
193
193
def get_cmap (self , cmap ):
@@ -218,8 +218,8 @@ def get_cmap(self, cmap):
218
218
# otherwise, it must be a string so look it up
219
219
return self [cmap ]
220
220
raise TypeError (
221
- " get_cmap expects None or an instance of a str or Colormap . "
222
- + f" you passed { cmap !r} of type { type (cmap )} "
221
+ ' get_cmap expects None or an instance of a str or Colormap . ' +
222
+ f' you passed { cmap !r} of type { type (cmap )} '
223
223
)
224
224
225
225
@@ -264,7 +264,8 @@ def register_cmap(name=None, cmap=None, *, override_builtin=False):
264
264
try :
265
265
name = cmap .name
266
266
except AttributeError as err :
267
- raise ValueError ("Arguments must include a name or a " "Colormap" ) from err
267
+ raise ValueError ("Arguments must include a name or a "
268
+ "Colormap" ) from err
268
269
# override_builtin is allowed here for backward compatibility
269
270
# this is just a shim to enable that to work privately in
270
271
# the global ColormapRegistry
@@ -292,7 +293,7 @@ def _get_cmap(name=None, lut=None):
292
293
Colormap
293
294
"""
294
295
if name is None :
295
- name = mpl .rcParams [" image.cmap" ]
296
+ name = mpl .rcParams [' image.cmap' ]
296
297
if isinstance (name , colors .Colormap ):
297
298
return name
298
299
_api .check_in_list (sorted (_colormaps ), name = name )
@@ -301,19 +302,20 @@ def _get_cmap(name=None, lut=None):
301
302
else :
302
303
return _colormaps [name ].resampled (lut )
303
304
304
-
305
305
# do it in two steps like this so we can have an un-deprecated version in
306
306
# pyplot.
307
307
get_cmap = _api .deprecated (
308
- " 3.7" ,
309
- name = " get_cmap" ,
308
+ ' 3.7' ,
309
+ name = ' get_cmap' ,
310
310
alternative = (
311
- "``matplotlib.colormaps[name]`` " + "or ``matplotlib.colormaps.get_cmap(obj)``"
312
- ),
311
+ "``matplotlib.colormaps[name]`` " +
312
+ "or ``matplotlib.colormaps.get_cmap(obj)``"
313
+ )
313
314
)(_get_cmap )
314
315
315
316
316
- @_api .deprecated ("3.7" , alternative = "``matplotlib.colormaps.unregister(name)``" )
317
+ @_api .deprecated ("3.7" ,
318
+ alternative = "``matplotlib.colormaps.unregister(name)``" )
317
319
def unregister_cmap (name ):
318
320
"""
319
321
Remove a colormap recognized by :func:`get_cmap`.
@@ -371,10 +373,11 @@ def _auto_norm_from_scale(scale_cls):
371
373
# ``nonpositive="mask"`` is supported.
372
374
try :
373
375
norm = colors .make_norm_from_scale (
374
- functools .partial (scale_cls , nonpositive = "mask" )
375
- )( colors .Normalize )()
376
+ functools .partial (scale_cls , nonpositive = "mask" ))(
377
+ colors .Normalize )()
376
378
except TypeError :
377
- norm = colors .make_norm_from_scale (scale_cls )(colors .Normalize )()
379
+ norm = colors .make_norm_from_scale (scale_cls )(
380
+ colors .Normalize )()
378
381
return type (norm )
379
382
380
383
@@ -425,8 +428,7 @@ def _scale_norm(self, norm, vmin, vmax):
425
428
raise ValueError (
426
429
"Passing a Normalize instance simultaneously with "
427
430
"vmin/vmax is not supported. Please pass vmin/vmax "
428
- "directly to the norm when creating it."
429
- )
431
+ "directly to the norm when creating it." )
430
432
431
433
# always resolve the autoscaling so we have concrete limits
432
434
# rather than deferring to draw time.
@@ -478,22 +480,18 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
478
480
xx = x
479
481
else :
480
482
raise ValueError ("Third dimension must be 3 or 4" )
481
- if xx .dtype .kind == "f" :
483
+ if xx .dtype .kind == 'f' :
482
484
if norm and (xx .max () > 1 or xx .min () < 0 ):
483
- raise ValueError (
484
- "Floating point image RGB values "
485
- "must be in the 0..1 range."
486
- )
485
+ raise ValueError ("Floating point image RGB values "
486
+ "must be in the 0..1 range." )
487
487
if bytes :
488
488
xx = (xx * 255 ).astype (np .uint8 )
489
489
elif xx .dtype == np .uint8 :
490
490
if not bytes :
491
491
xx = xx .astype (np .float32 ) / 255
492
492
else :
493
- raise ValueError (
494
- "Image RGB array must be uint8 or "
495
- "floating point; found %s" % xx .dtype
496
- )
493
+ raise ValueError ("Image RGB array must be uint8 or "
494
+ "floating point; found %s" % xx .dtype )
497
495
return xx
498
496
except AttributeError :
499
497
# e.g., x is not an ndarray; so try mapping it
@@ -524,9 +522,8 @@ def set_array(self, A):
524
522
525
523
A = cbook .safe_masked_invalid (A , copy = True )
526
524
if not np .can_cast (A .dtype , float , "same_kind" ):
527
- raise TypeError (
528
- f"Image data of dtype { A .dtype } cannot be " "converted to float"
529
- )
525
+ raise TypeError (f"Image data of dtype { A .dtype } cannot be "
526
+ "converted to float" )
530
527
531
528
self ._A = A
532
529
@@ -583,7 +580,7 @@ def get_alpha(self):
583
580
Always returns 1.
584
581
"""
585
582
# This method is intended to be overridden by Artist sub-classes
586
- return 1.0
583
+ return 1.
587
584
588
585
def set_cmap (self , cmap ):
589
586
"""
@@ -627,7 +624,8 @@ def norm(self, norm):
627
624
if not in_init :
628
625
self .norm .callbacks .disconnect (self ._id_norm )
629
626
self ._norm = norm
630
- self ._id_norm = self .norm .callbacks .connect ("changed" , self .changed )
627
+ self ._id_norm = self .norm .callbacks .connect ('changed' ,
628
+ self .changed )
631
629
if not in_init :
632
630
self .changed ()
633
631
@@ -653,7 +651,7 @@ def autoscale(self):
653
651
current array
654
652
"""
655
653
if self ._A is None :
656
- raise TypeError (" You must first set_array for mappable" )
654
+ raise TypeError (' You must first set_array for mappable' )
657
655
# If the norm's limits are updated self.changed() will be called
658
656
# through the callbacks attached to the norm
659
657
self .norm .autoscale (self ._A )
@@ -664,7 +662,7 @@ def autoscale_None(self):
664
662
current array, changing only limits that are None
665
663
"""
666
664
if self ._A is None :
667
- raise TypeError (" You must first set_array for mappable" )
665
+ raise TypeError (' You must first set_array for mappable' )
668
666
# If the norm's limits are updated self.changed() will be called
669
667
# through the callbacks attached to the norm
670
668
self .norm .autoscale_None (self ._A )
@@ -674,7 +672,7 @@ def changed(self):
674
672
Call this whenever the mappable is changed to notify all the
675
673
callbackSM listeners to the 'changed' signal.
676
674
"""
677
- self .callbacks .process (" changed" , self )
675
+ self .callbacks .process (' changed' , self )
678
676
self .stale = True
679
677
680
678
0 commit comments