2626from matplotlib ._cm_listed import cmaps as cmaps_listed
2727
2828
29- _LUTSIZE = mpl .rcParams [" image.lut" ]
29+ _LUTSIZE = mpl .rcParams [' image.lut' ]
3030
3131
3232def _gen_cmap_registry ():
@@ -38,22 +38,20 @@ def _gen_cmap_registry():
3838 for name , spec in datad .items ():
3939 cmap_d [name ] = ( # Precache the cmaps at a fixed lutsize..
4040 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 ))
4645
47- # Register colormap aliases for gray and grey.
46+ # Register colormap aliases for gray and grey.
4847 cmap_d ["grey" ] = cmap_d ["gray" ]
49- cmap_d ["Greys" ] = cmap_d ["Grays" ]
5048 cmap_d ["gist_grey" ] = cmap_d ["gist_gray" ]
49+ cmap_d ["Grays" ] = cmap_d ["Greys" ]
5150
5251 # Generate reversed cmaps.
5352 for cmap in list (cmap_d .values ()):
5453 rmap = cmap .reversed ()
5554 cmap_d [rmap .name ] = rmap
56-
5755 return cmap_d
5856
5957
@@ -76,7 +74,6 @@ class ColormapRegistry(Mapping):
7674
7775 mpl.colormaps.register(my_colormap)
7876 """
79-
8077 def __init__ (self , cmaps ):
8178 self ._cmaps = cmaps
8279 self ._builtin_cmaps = tuple (cmaps )
@@ -96,9 +93,8 @@ def __len__(self):
9693 return len (self ._cmaps )
9794
9895 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 ))
10298
10399 def __call__ (self ):
104100 """
@@ -142,20 +138,25 @@ def register(self, cmap, *, name=None, force=False):
142138 if not force :
143139 # don't allow registering an already existing cmap
144140 # 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 ):
147145 # We don't allow overriding a builtin unless privately
148146 # 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." )
152149
153150 # 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." )
157153
158154 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
159160
160161 def unregister (self , name ):
161162 """
@@ -185,9 +186,8 @@ def unregister(self, name):
185186 If you try to remove a default built-in colormap.
186187 """
187188 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." )
191191 self ._cmaps .pop (name , None )
192192
193193 def get_cmap (self , cmap ):
@@ -218,8 +218,8 @@ def get_cmap(self, cmap):
218218 # otherwise, it must be a string so look it up
219219 return self [cmap ]
220220 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 )} '
223223 )
224224
225225
@@ -264,7 +264,8 @@ def register_cmap(name=None, cmap=None, *, override_builtin=False):
264264 try :
265265 name = cmap .name
266266 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
268269 # override_builtin is allowed here for backward compatibility
269270 # this is just a shim to enable that to work privately in
270271 # the global ColormapRegistry
@@ -292,7 +293,7 @@ def _get_cmap(name=None, lut=None):
292293 Colormap
293294 """
294295 if name is None :
295- name = mpl .rcParams [" image.cmap" ]
296+ name = mpl .rcParams [' image.cmap' ]
296297 if isinstance (name , colors .Colormap ):
297298 return name
298299 _api .check_in_list (sorted (_colormaps ), name = name )
@@ -301,19 +302,20 @@ def _get_cmap(name=None, lut=None):
301302 else :
302303 return _colormaps [name ].resampled (lut )
303304
304-
305305# do it in two steps like this so we can have an un-deprecated version in
306306# pyplot.
307307get_cmap = _api .deprecated (
308- " 3.7" ,
309- name = " get_cmap" ,
308+ ' 3.7' ,
309+ name = ' get_cmap' ,
310310 alternative = (
311- "``matplotlib.colormaps[name]`` " + "or ``matplotlib.colormaps.get_cmap(obj)``"
312- ),
311+ "``matplotlib.colormaps[name]`` " +
312+ "or ``matplotlib.colormaps.get_cmap(obj)``"
313+ )
313314)(_get_cmap )
314315
315316
316- @_api .deprecated ("3.7" , alternative = "``matplotlib.colormaps.unregister(name)``" )
317+ @_api .deprecated ("3.7" ,
318+ alternative = "``matplotlib.colormaps.unregister(name)``" )
317319def unregister_cmap (name ):
318320 """
319321 Remove a colormap recognized by :func:`get_cmap`.
@@ -371,10 +373,11 @@ def _auto_norm_from_scale(scale_cls):
371373 # ``nonpositive="mask"`` is supported.
372374 try :
373375 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 )()
376378 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 )()
378381 return type (norm )
379382
380383
@@ -425,8 +428,7 @@ def _scale_norm(self, norm, vmin, vmax):
425428 raise ValueError (
426429 "Passing a Normalize instance simultaneously with "
427430 "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." )
430432
431433 # always resolve the autoscaling so we have concrete limits
432434 # rather than deferring to draw time.
@@ -478,22 +480,18 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
478480 xx = x
479481 else :
480482 raise ValueError ("Third dimension must be 3 or 4" )
481- if xx .dtype .kind == "f" :
483+ if xx .dtype .kind == 'f' :
482484 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." )
487487 if bytes :
488488 xx = (xx * 255 ).astype (np .uint8 )
489489 elif xx .dtype == np .uint8 :
490490 if not bytes :
491491 xx = xx .astype (np .float32 ) / 255
492492 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 )
497495 return xx
498496 except AttributeError :
499497 # e.g., x is not an ndarray; so try mapping it
@@ -524,9 +522,8 @@ def set_array(self, A):
524522
525523 A = cbook .safe_masked_invalid (A , copy = True )
526524 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" )
530527
531528 self ._A = A
532529
@@ -583,7 +580,7 @@ def get_alpha(self):
583580 Always returns 1.
584581 """
585582 # This method is intended to be overridden by Artist sub-classes
586- return 1.0
583+ return 1.
587584
588585 def set_cmap (self , cmap ):
589586 """
@@ -627,7 +624,8 @@ def norm(self, norm):
627624 if not in_init :
628625 self .norm .callbacks .disconnect (self ._id_norm )
629626 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 )
631629 if not in_init :
632630 self .changed ()
633631
@@ -653,7 +651,7 @@ def autoscale(self):
653651 current array
654652 """
655653 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' )
657655 # If the norm's limits are updated self.changed() will be called
658656 # through the callbacks attached to the norm
659657 self .norm .autoscale (self ._A )
@@ -664,7 +662,7 @@ def autoscale_None(self):
664662 current array, changing only limits that are None
665663 """
666664 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' )
668666 # If the norm's limits are updated self.changed() will be called
669667 # through the callbacks attached to the norm
670668 self .norm .autoscale_None (self ._A )
@@ -674,7 +672,7 @@ def changed(self):
674672 Call this whenever the mappable is changed to notify all the
675673 callbackSM listeners to the 'changed' signal.
676674 """
677- self .callbacks .process (" changed" , self )
675+ self .callbacks .process (' changed' , self )
678676 self .stale = True
679677
680678
0 commit comments