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

Skip to content

Backport PR #29590 on branch v3.10.x (Blocked set_clim() callbacks to prevent inconsistent state (#29522)) #29693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions lib/matplotlib/colorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,27 @@ def set_clim(self, vmin=None, vmax=None):
.. ACCEPTS: (vmin: float, vmax: float)
"""
# If the norm's limits are updated self.changed() will be called
# through the callbacks attached to the norm
# through the callbacks attached to the norm, this causes an inconsistent
# state, to prevent this blocked context manager is used
if vmax is None:
try:
vmin, vmax = vmin
except (TypeError, ValueError):
pass
if vmin is not None:
self.norm.vmin = colors._sanitize_extrema(vmin)
if vmax is not None:
self.norm.vmax = colors._sanitize_extrema(vmax)

orig_vmin_vmax = self.norm.vmin, self.norm.vmax

# Blocked context manager prevents callbacks from being triggered
# until both vmin and vmax are updated
with self.norm.callbacks.blocked(signal='changed'):
if vmin is not None:
self.norm.vmin = colors._sanitize_extrema(vmin)
if vmax is not None:
self.norm.vmax = colors._sanitize_extrema(vmax)

# emit a update signal if the limits are changed
if orig_vmin_vmax != (self.norm.vmin, self.norm.vmax):
self.norm.callbacks.process('changed')

def get_clim(self):
"""
Expand Down
17 changes: 17 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,23 @@ def test_norm_deepcopy():
assert norm2.vmin == norm.vmin


def test_set_clim_emits_single_callback():
data = np.array([[1, 2], [3, 4]])
fig, ax = plt.subplots()
image = ax.imshow(data, cmap='viridis')

callback = unittest.mock.Mock()
image.norm.callbacks.connect('changed', callback)

callback.assert_not_called()

# Call set_clim() to update the limits
image.set_clim(1, 5)

# Assert that only one "changed" callback is sent after calling set_clim()
callback.assert_called_once()


def test_norm_callback():
increment = unittest.mock.Mock(return_value=None)

Expand Down
Loading