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,21 +38,22 @@ 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 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
+
45
52
# Generate reversed cmaps.
46
53
for cmap in list (cmap_d .values ()):
47
54
rmap = cmap .reversed ()
48
55
cmap_d [rmap .name ] = rmap
49
56
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
-
56
57
return cmap_d
57
58
58
59
@@ -75,6 +76,7 @@ class ColormapRegistry(Mapping):
75
76
76
77
mpl.colormaps.register(my_colormap)
77
78
"""
79
+
78
80
def __init__ (self , cmaps ):
79
81
self ._cmaps = cmaps
80
82
self ._builtin_cmaps = tuple (cmaps )
@@ -94,8 +96,9 @@ def __len__(self):
94
96
return len (self ._cmaps )
95
97
96
98
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
+ )
99
102
100
103
def __call__ (self ):
101
104
"""
@@ -139,18 +142,18 @@ def register(self, cmap, *, name=None, force=False):
139
142
if not force :
140
143
# don't allow registering an already existing cmap
141
144
# 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 :
146
147
# We don't allow overriding a builtin unless privately
147
148
# 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
+ )
150
152
151
153
# 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
+ )
154
157
155
158
self ._cmaps [name ] = cmap .copy ()
156
159
@@ -182,8 +185,9 @@ def unregister(self, name):
182
185
If you try to remove a default built-in colormap.
183
186
"""
184
187
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
+ )
187
191
self ._cmaps .pop (name , None )
188
192
189
193
def get_cmap (self , cmap ):
@@ -214,8 +218,8 @@ def get_cmap(self, cmap):
214
218
# otherwise, it must be a string so look it up
215
219
return self [cmap ]
216
220
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 )} "
219
223
)
220
224
221
225
@@ -260,8 +264,7 @@ def register_cmap(name=None, cmap=None, *, override_builtin=False):
260
264
try :
261
265
name = cmap .name
262
266
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
265
268
# override_builtin is allowed here for backward compatibility
266
269
# this is just a shim to enable that to work privately in
267
270
# the global ColormapRegistry
@@ -289,7 +292,7 @@ def _get_cmap(name=None, lut=None):
289
292
Colormap
290
293
"""
291
294
if name is None :
292
- name = mpl .rcParams [' image.cmap' ]
295
+ name = mpl .rcParams [" image.cmap" ]
293
296
if isinstance (name , colors .Colormap ):
294
297
return name
295
298
_api .check_in_list (sorted (_colormaps ), name = name )
@@ -298,20 +301,19 @@ def _get_cmap(name=None, lut=None):
298
301
else :
299
302
return _colormaps [name ].resampled (lut )
300
303
304
+
301
305
# do it in two steps like this so we can have an un-deprecated version in
302
306
# pyplot.
303
307
get_cmap = _api .deprecated (
304
- ' 3.7' ,
305
- name = ' get_cmap' ,
308
+ " 3.7" ,
309
+ name = " get_cmap" ,
306
310
alternative = (
307
- "``matplotlib.colormaps[name]`` " +
308
- "or ``matplotlib.colormaps.get_cmap(obj)``"
309
- )
311
+ "``matplotlib.colormaps[name]`` " + "or ``matplotlib.colormaps.get_cmap(obj)``"
312
+ ),
310
313
)(_get_cmap )
311
314
312
315
313
- @_api .deprecated ("3.7" ,
314
- alternative = "``matplotlib.colormaps.unregister(name)``" )
316
+ @_api .deprecated ("3.7" , alternative = "``matplotlib.colormaps.unregister(name)``" )
315
317
def unregister_cmap (name ):
316
318
"""
317
319
Remove a colormap recognized by :func:`get_cmap`.
@@ -369,11 +371,10 @@ def _auto_norm_from_scale(scale_cls):
369
371
# ``nonpositive="mask"`` is supported.
370
372
try :
371
373
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 )()
374
376
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 )()
377
378
return type (norm )
378
379
379
380
@@ -424,7 +425,8 @@ def _scale_norm(self, norm, vmin, vmax):
424
425
raise ValueError (
425
426
"Passing a Normalize instance simultaneously with "
426
427
"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
+ )
428
430
429
431
# always resolve the autoscaling so we have concrete limits
430
432
# rather than deferring to draw time.
@@ -476,18 +478,22 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
476
478
xx = x
477
479
else :
478
480
raise ValueError ("Third dimension must be 3 or 4" )
479
- if xx .dtype .kind == 'f' :
481
+ if xx .dtype .kind == "f" :
480
482
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
+ )
483
487
if bytes :
484
488
xx = (xx * 255 ).astype (np .uint8 )
485
489
elif xx .dtype == np .uint8 :
486
490
if not bytes :
487
491
xx = xx .astype (np .float32 ) / 255
488
492
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
+ )
491
497
return xx
492
498
except AttributeError :
493
499
# e.g., x is not an ndarray; so try mapping it
@@ -518,8 +524,9 @@ def set_array(self, A):
518
524
519
525
A = cbook .safe_masked_invalid (A , copy = True )
520
526
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
+ )
523
530
524
531
self ._A = A
525
532
@@ -576,7 +583,7 @@ def get_alpha(self):
576
583
Always returns 1.
577
584
"""
578
585
# This method is intended to be overridden by Artist sub-classes
579
- return 1.
586
+ return 1.0
580
587
581
588
def set_cmap (self , cmap ):
582
589
"""
@@ -620,8 +627,7 @@ def norm(self, norm):
620
627
if not in_init :
621
628
self .norm .callbacks .disconnect (self ._id_norm )
622
629
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 )
625
631
if not in_init :
626
632
self .changed ()
627
633
@@ -647,7 +653,7 @@ def autoscale(self):
647
653
current array
648
654
"""
649
655
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" )
651
657
# If the norm's limits are updated self.changed() will be called
652
658
# through the callbacks attached to the norm
653
659
self .norm .autoscale (self ._A )
@@ -658,7 +664,7 @@ def autoscale_None(self):
658
664
current array, changing only limits that are None
659
665
"""
660
666
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" )
662
668
# If the norm's limits are updated self.changed() will be called
663
669
# through the callbacks attached to the norm
664
670
self .norm .autoscale_None (self ._A )
@@ -668,7 +674,7 @@ def changed(self):
668
674
Call this whenever the mappable is changed to notify all the
669
675
callbackSM listeners to the 'changed' signal.
670
676
"""
671
- self .callbacks .process (' changed' , self )
677
+ self .callbacks .process (" changed" , self )
672
678
self .stale = True
673
679
674
680
0 commit comments