Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56fba40

Browse files
committed
Validate inputs to ScalarMappable constructor.
1 parent fb7cf79 commit 56fba40

1 file changed

Lines changed: 16 additions & 19 deletions

File tree

lib/matplotlib/cm.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,14 @@ def __init__(self, norm=None, cmap=None):
167167
cmap : str or `~matplotlib.colors.Colormap`
168168
The colormap used to map normalized data values to RGBA colors.
169169
"""
170-
171-
self.callbacksSM = cbook.CallbackRegistry()
172-
173-
if cmap is None:
174-
cmap = get_cmap()
175-
if norm is None:
176-
norm = colors.Normalize()
177-
178170
self._A = None
179-
#: The Normalization instance of this ScalarMappable.
180-
self.norm = norm
181-
#: The Colormap instance of this ScalarMappable.
182-
self.cmap = get_cmap(cmap)
171+
self.norm = None # So that the setter knows we're initializing.
172+
self.set_norm(norm) # The Normalize instance of this ScalarMappable.
173+
self.cmap = None # So that the setter knows we're initializing.
174+
self.set_cmap(cmap) # The Colormap instance of this ScalarMappable.
183175
#: The last colorbar associated with this ScalarMappable. May be None.
184176
self.colorbar = None
177+
self.callbacksSM = cbook.CallbackRegistry()
185178
self.update_dict = {'array': False}
186179

187180
def _scale_norm(self, norm, vmin, vmax):
@@ -336,35 +329,39 @@ def get_alpha(self):
336329

337330
def set_cmap(self, cmap):
338331
"""
339-
set the colormap for luminance data
332+
Set the colormap for luminance data.
340333
341334
Parameters
342335
----------
343-
cmap : colormap or registered colormap name
336+
cmap : `.Colormap` or str or None
344337
"""
338+
in_init = self.cmap is None
345339
cmap = get_cmap(cmap)
346340
self.cmap = cmap
347-
self.changed()
341+
if not in_init:
342+
self.changed() # Things are not set up properly yet.
348343

349344
def set_norm(self, norm):
350-
"""Set the normalization instance.
345+
"""
346+
Set the normalization instance.
351347
352348
Parameters
353349
----------
354-
norm : `.Normalize`
350+
norm : `.Normalize` or None
355351
356352
Notes
357353
-----
358354
If there are any colorbars using the mappable for this norm, setting
359355
the norm of the mappable will reset the norm, locator, and formatters
360356
on the colorbar to default.
361-
362357
"""
363358
cbook._check_isinstance((colors.Normalize, None), norm=norm)
359+
in_init = self.norm is None
364360
if norm is None:
365361
norm = colors.Normalize()
366362
self.norm = norm
367-
self.changed()
363+
if not in_init:
364+
self.changed() # Things are not set up properly yet.
368365

369366
def autoscale(self):
370367
"""

0 commit comments

Comments
 (0)