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

Commit365dd59

Browse files
authored
Merge pull request#29693 from meeseeksmachine/auto-backport-of-pr-29590-on-v3.10.x
Backport PR#29590 on branch v3.10.x (Blocked set_clim() callbacks to prevent inconsistent state (#29522))
2 parentsc38e0f7 +bf64054 commit365dd59

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

‎lib/matplotlib/colorizer.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,27 @@ def set_clim(self, vmin=None, vmax=None):
261261
.. ACCEPTS: (vmin: float, vmax: float)
262262
"""
263263
# If the norm's limits are updated self.changed() will be called
264-
# through the callbacks attached to the norm
264+
# through the callbacks attached to the norm, this causes an inconsistent
265+
# state, to prevent this blocked context manager is used
265266
ifvmaxisNone:
266267
try:
267268
vmin,vmax=vmin
268269
except (TypeError,ValueError):
269270
pass
270-
ifvminisnotNone:
271-
self.norm.vmin=colors._sanitize_extrema(vmin)
272-
ifvmaxisnotNone:
273-
self.norm.vmax=colors._sanitize_extrema(vmax)
271+
272+
orig_vmin_vmax=self.norm.vmin,self.norm.vmax
273+
274+
# Blocked context manager prevents callbacks from being triggered
275+
# until both vmin and vmax are updated
276+
withself.norm.callbacks.blocked(signal='changed'):
277+
ifvminisnotNone:
278+
self.norm.vmin=colors._sanitize_extrema(vmin)
279+
ifvmaxisnotNone:
280+
self.norm.vmax=colors._sanitize_extrema(vmax)
281+
282+
# emit a update signal if the limits are changed
283+
iforig_vmin_vmax!= (self.norm.vmin,self.norm.vmax):
284+
self.norm.callbacks.process('changed')
274285

275286
defget_clim(self):
276287
"""

‎lib/matplotlib/tests/test_colors.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,23 @@ def test_norm_deepcopy():
15531553
assertnorm2.vmin==norm.vmin
15541554

15551555

1556+
deftest_set_clim_emits_single_callback():
1557+
data=np.array([[1,2], [3,4]])
1558+
fig,ax=plt.subplots()
1559+
image=ax.imshow(data,cmap='viridis')
1560+
1561+
callback=unittest.mock.Mock()
1562+
image.norm.callbacks.connect('changed',callback)
1563+
1564+
callback.assert_not_called()
1565+
1566+
# Call set_clim() to update the limits
1567+
image.set_clim(1,5)
1568+
1569+
# Assert that only one "changed" callback is sent after calling set_clim()
1570+
callback.assert_called_once()
1571+
1572+
15561573
deftest_norm_callback():
15571574
increment=unittest.mock.Mock(return_value=None)
15581575

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp