Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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'):
Comment thread
prafulgulani marked this conversation as resolved.
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
20 changes: 20 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,26 @@ 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)

# Initial callback count should be zero
assert callback.call_count == 0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Initial callback count should be zero
assert callback.call_count == 0
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()

plt.close(fig)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary; tests always close figures.

Suggested change
plt.close(fig)


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

Expand Down