Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5f7f4b7

Browse filesBrowse files
committed
FIX: Update the callback in scalar mappable on norm change
When changing a ScalarMappable's norm, the previous norm callback wasn't being removed and updated with the new one. This adds that capability to the norm setter.
1 parent d663626 commit 5f7f4b7
Copy full SHA for 5f7f4b7

File tree

Expand file treeCollapse file tree

2 files changed

+29
-12
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+29
-12
lines changed

‎lib/matplotlib/cm.py

Copy file name to clipboardExpand all lines: lib/matplotlib/cm.py
+25-10Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,13 @@ def __init__(self, norm=None, cmap=None):
257257
The colormap used to map normalized data values to RGBA colors.
258258
"""
259259
self._A = None
260-
self.norm = None # So that the setter knows we're initializing.
260+
self._norm = None # So that the setter knows we're initializing.
261261
self.set_norm(norm) # The Normalize instance of this ScalarMappable.
262262
self.cmap = None # So that the setter knows we're initializing.
263263
self.set_cmap(cmap) # The Colormap instance of this ScalarMappable.
264264
#: The last colorbar associated with this ScalarMappable. May be None.
265265
self.colorbar = None
266266
self.callbacksSM = cbook.CallbackRegistry()
267-
# Connect to the Norm's callback
268-
self._id_norm = self.norm.callbacksNorm.connect('changed',
269-
self.changed)
270267

271268
def _scale_norm(self, norm, vmin, vmax):
272269
"""
@@ -436,6 +433,30 @@ def set_cmap(self, cmap):
436433
if not in_init:
437434
self.changed() # Things are not set up properly yet.
438435

436+
@property
437+
def norm(self):
438+
return self._norm
439+
440+
@norm.setter
441+
def norm(self, norm):
442+
_api.check_isinstance((colors.Normalize, None), norm=norm)
443+
if norm is None:
444+
norm = colors.Normalize()
445+
446+
if norm is self.norm:
447+
# We aren't updating anything
448+
return
449+
450+
in_init = self.norm is None
451+
# Remove the current callback and connect to the new one
452+
if not in_init:
453+
self.norm.callbacksNorm.disconnect(self._id_norm)
454+
self._norm = norm
455+
self._id_norm = self.norm.callbacksNorm.connect('changed',
456+
self.changed)
457+
if not in_init:
458+
self.changed()
459+
439460
def set_norm(self, norm):
440461
"""
441462
Set the normalization instance.
@@ -450,13 +471,7 @@ def set_norm(self, norm):
450471
the norm of the mappable will reset the norm, locator, and formatters
451472
on the colorbar to default.
452473
"""
453-
_api.check_isinstance((colors.Normalize, None), norm=norm)
454-
in_init = self.norm is None
455-
if norm is None:
456-
norm = colors.Normalize()
457474
self.norm = norm
458-
if not in_init:
459-
self.changed() # Things are not set up properly yet.
460475

461476
def autoscale(self):
462477
"""

‎lib/matplotlib/tests/test_colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colors.py
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,13 +1421,15 @@ def test_scalarmappable_norm_update():
14211421
norm.clip = True
14221422
assert sm.stale
14231423
# change to the CenteredNorm and TwoSlopeNorm to test those
1424+
# Also make sure that updating the norm directly and with
1425+
# set_norm both update the Norm callback
14241426
norm = mcolors.CenteredNorm()
1425-
sm = matplotlib.cm.ScalarMappable(norm=norm, cmap='plasma')
1427+
sm.norm = norm
14261428
sm.stale = False
14271429
norm.vcenter = 1
14281430
assert sm.stale
14291431
norm = mcolors.TwoSlopeNorm(vcenter=0, vmin=-1, vmax=1)
1430-
sm = matplotlib.cm.ScalarMappable(norm=norm, cmap='plasma')
1432+
sm.set_norm(norm)
14311433
sm.stale = False
14321434
norm.vcenter = 1
14331435
assert sm.stale

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.