Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Blocked set_clim() callbacks to prevent inconsistent state (#29522)#29590

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionslib/matplotlib/colorizer.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionslib/matplotlib/tests/test_colors.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1605,6 +1605,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

[8]ページ先頭

©2009-2025 Movatter.jp