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,21 +38,22 @@ 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 else
42- colors .ListedColormap (spec ['listed' ], name )
43- if 'listed' in spec else
44- colors .LinearSegmentedColormap .from_list (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+ )
46+
47+ # Register colormap aliases for gray and grey.
48+ cmap_d ["grey" ] = cmap_d ["gray" ]
49+ cmap_d ["Greys" ] = cmap_d ["Grays" ]
50+ cmap_d ["gist_grey" ] = cmap_d ["gist_gray" ]
51+
4552 # Generate reversed cmaps.
4653 for cmap in list (cmap_d .values ()):
4754 rmap = cmap .reversed ()
4855 cmap_d [rmap .name ] = rmap
4956
50- # Register colormap aliases for gray and grey.
51- cmap_d ['grey' ] = cmap_d ['gist_grey' ] = cmap_d ['gray' ]
52- cmap_d ['Grays' ] = cmap_d ['Greys' ]
53- cmap_d ['gist_yerg' ] = cmap_d ['gist_yarg' ] = cmap_d ['gray_r' ]
54- cmap_d ['grey_r' ] = cmap_d ['gray_r' ]
55-
5657 return cmap_d
5758
5859
@@ -75,6 +76,7 @@ class ColormapRegistry(Mapping):
7576
7677 mpl.colormaps.register(my_colormap)
7778 """
79+
7880 def __init__ (self , cmaps ):
7981 self ._cmaps = cmaps
8082 self ._builtin_cmaps = tuple (cmaps )
@@ -94,8 +96,9 @@ def __len__(self):
9496 return len (self ._cmaps )
9597
9698 def __str__ (self ):
97- return ('ColormapRegistry; available colormaps:\n ' +
98- ', ' .join (f"'{ name } '" for name in self ))
99+ return "ColormapRegistry; available colormaps:\n " + ", " .join (
100+ f"'{ name } '" for name in self
101+ )
99102
100103 def __call__ (self ):
101104 """
@@ -139,18 +142,18 @@ def register(self, cmap, *, name=None, force=False):
139142 if not force :
140143 # don't allow registering an already existing cmap
141144 # unless explicitly asked to
142- raise ValueError (
143- f'A colormap named "{ name } " is already registered.' )
144- elif (name in self ._builtin_cmaps
145- and not self ._allow_override_builtin ):
145+ raise ValueError (f'A colormap named "{ name } " is already registered.' )
146+ elif name in self ._builtin_cmaps and not self ._allow_override_builtin :
146147 # We don't allow overriding a builtin unless privately
147148 # coming from register_cmap()
148- raise ValueError ("Re-registering the builtin cmap "
149- f"{ name !r} is not allowed." )
149+ raise ValueError (
150+ "Re-registering the builtin cmap " f"{ name !r} is not allowed."
151+ )
150152
151153 # Warn that we are updating an already existing colormap
152- _api .warn_external (f"Overwriting the cmap { name !r} "
153- "that was already in the registry." )
154+ _api .warn_external (
155+ f"Overwriting the cmap { name !r} " "that was already in the registry."
156+ )
154157
155158 self ._cmaps [name ] = cmap .copy ()
156159
@@ -182,8 +185,9 @@ def unregister(self, name):
182185 If you try to remove a default built-in colormap.
183186 """
184187 if name in self ._builtin_cmaps :
185- raise ValueError (f"cannot unregister { name !r} which is a builtin "
186- "colormap." )
188+ raise ValueError (
189+ f"cannot unregister { name !r} which is a builtin " "colormap."
190+ )
187191 self ._cmaps .pop (name , None )
188192
189193 def get_cmap (self , cmap ):
@@ -214,8 +218,8 @@ def get_cmap(self, cmap):
214218 # otherwise, it must be a string so look it up
215219 return self [cmap ]
216220 raise TypeError (
217- ' get_cmap expects None or an instance of a str or Colormap . ' +
218- 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 )} "
219223 )
220224
221225
@@ -260,8 +264,7 @@ def register_cmap(name=None, cmap=None, *, override_builtin=False):
260264 try :
261265 name = cmap .name
262266 except AttributeError as err :
263- raise ValueError ("Arguments must include a name or a "
264- "Colormap" ) from err
267+ raise ValueError ("Arguments must include a name or a " "Colormap" ) from err
265268 # override_builtin is allowed here for backward compatibility
266269 # this is just a shim to enable that to work privately in
267270 # the global ColormapRegistry
@@ -289,7 +292,7 @@ def _get_cmap(name=None, lut=None):
289292 Colormap
290293 """
291294 if name is None :
292- name = mpl .rcParams [' image.cmap' ]
295+ name = mpl .rcParams [" image.cmap" ]
293296 if isinstance (name , colors .Colormap ):
294297 return name
295298 _api .check_in_list (sorted (_colormaps ), name = name )
@@ -298,20 +301,19 @@ def _get_cmap(name=None, lut=None):
298301 else :
299302 return _colormaps [name ].resampled (lut )
300303
304+
301305# do it in two steps like this so we can have an un-deprecated version in
302306# pyplot.
303307get_cmap = _api .deprecated (
304- ' 3.7' ,
305- name = ' get_cmap' ,
308+ " 3.7" ,
309+ name = " get_cmap" ,
306310 alternative = (
307- "``matplotlib.colormaps[name]`` " +
308- "or ``matplotlib.colormaps.get_cmap(obj)``"
309- )
311+ "``matplotlib.colormaps[name]`` " + "or ``matplotlib.colormaps.get_cmap(obj)``"
312+ ),
310313)(_get_cmap )
311314
312315
313- @_api .deprecated ("3.7" ,
314- alternative = "``matplotlib.colormaps.unregister(name)``" )
316+ @_api .deprecated ("3.7" , alternative = "``matplotlib.colormaps.unregister(name)``" )
315317def unregister_cmap (name ):
316318 """
317319 Remove a colormap recognized by :func:`get_cmap`.
@@ -369,11 +371,10 @@ def _auto_norm_from_scale(scale_cls):
369371 # ``nonpositive="mask"`` is supported.
370372 try :
371373 norm = colors .make_norm_from_scale (
372- functools .partial (scale_cls , nonpositive = "mask" ))(
373- colors .Normalize )()
374+ functools .partial (scale_cls , nonpositive = "mask" )
375+ )( colors .Normalize )()
374376 except TypeError :
375- norm = colors .make_norm_from_scale (scale_cls )(
376- colors .Normalize )()
377+ norm = colors .make_norm_from_scale (scale_cls )(colors .Normalize )()
377378 return type (norm )
378379
379380
@@ -424,7 +425,8 @@ def _scale_norm(self, norm, vmin, vmax):
424425 raise ValueError (
425426 "Passing a Normalize instance simultaneously with "
426427 "vmin/vmax is not supported. Please pass vmin/vmax "
427- "directly to the norm when creating it." )
428+ "directly to the norm when creating it."
429+ )
428430
429431 # always resolve the autoscaling so we have concrete limits
430432 # rather than deferring to draw time.
@@ -476,18 +478,22 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
476478 xx = x
477479 else :
478480 raise ValueError ("Third dimension must be 3 or 4" )
479- if xx .dtype .kind == 'f' :
481+ if xx .dtype .kind == "f" :
480482 if norm and (xx .max () > 1 or xx .min () < 0 ):
481- raise ValueError ("Floating point image RGB values "
482- "must be in the 0..1 range." )
483+ raise ValueError (
484+ "Floating point image RGB values "
485+ "must be in the 0..1 range."
486+ )
483487 if bytes :
484488 xx = (xx * 255 ).astype (np .uint8 )
485489 elif xx .dtype == np .uint8 :
486490 if not bytes :
487491 xx = xx .astype (np .float32 ) / 255
488492 else :
489- raise ValueError ("Image RGB array must be uint8 or "
490- "floating point; found %s" % xx .dtype )
493+ raise ValueError (
494+ "Image RGB array must be uint8 or "
495+ "floating point; found %s" % xx .dtype
496+ )
491497 return xx
492498 except AttributeError :
493499 # e.g., x is not an ndarray; so try mapping it
@@ -518,8 +524,9 @@ def set_array(self, A):
518524
519525 A = cbook .safe_masked_invalid (A , copy = True )
520526 if not np .can_cast (A .dtype , float , "same_kind" ):
521- raise TypeError (f"Image data of dtype { A .dtype } cannot be "
522- "converted to float" )
527+ raise TypeError (
528+ f"Image data of dtype { A .dtype } cannot be " "converted to float"
529+ )
523530
524531 self ._A = A
525532
@@ -576,7 +583,7 @@ def get_alpha(self):
576583 Always returns 1.
577584 """
578585 # This method is intended to be overridden by Artist sub-classes
579- return 1.
586+ return 1.0
580587
581588 def set_cmap (self , cmap ):
582589 """
@@ -620,8 +627,7 @@ def norm(self, norm):
620627 if not in_init :
621628 self .norm .callbacks .disconnect (self ._id_norm )
622629 self ._norm = norm
623- self ._id_norm = self .norm .callbacks .connect ('changed' ,
624- self .changed )
630+ self ._id_norm = self .norm .callbacks .connect ("changed" , self .changed )
625631 if not in_init :
626632 self .changed ()
627633
@@ -647,7 +653,7 @@ def autoscale(self):
647653 current array
648654 """
649655 if self ._A is None :
650- raise TypeError (' You must first set_array for mappable' )
656+ raise TypeError (" You must first set_array for mappable" )
651657 # If the norm's limits are updated self.changed() will be called
652658 # through the callbacks attached to the norm
653659 self .norm .autoscale (self ._A )
@@ -658,7 +664,7 @@ def autoscale_None(self):
658664 current array, changing only limits that are None
659665 """
660666 if self ._A is None :
661- raise TypeError (' You must first set_array for mappable' )
667+ raise TypeError (" You must first set_array for mappable" )
662668 # If the norm's limits are updated self.changed() will be called
663669 # through the callbacks attached to the norm
664670 self .norm .autoscale_None (self ._A )
@@ -668,7 +674,7 @@ def changed(self):
668674 Call this whenever the mappable is changed to notify all the
669675 callbackSM listeners to the 'changed' signal.
670676 """
671- self .callbacks .process (' changed' , self )
677+ self .callbacks .process (" changed" , self )
672678 self .stale = True
673679
674680
0 commit comments