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

Skip to content

Commit 6bf98fa

Browse files
committed
Make Figure._axobservers a CallbackRegistry.
This avoids having to reset it on pickling/unpickling, as CallbackRegistry already handles that itself. Also if we later make CallbackRegistry support pickling/unpickling some callbacks (which I plan to do), axobservers will also benefit from that.
1 parent af49c1b commit 6bf98fa

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

lib/matplotlib/figure.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,8 +1622,7 @@ def _break_share_link(ax, grouper):
16221622
return None
16231623

16241624
self._axstack.remove(ax)
1625-
for func in self._axobservers:
1626-
func(self)
1625+
self._axobservers.process("_axes_change_event", self)
16271626
self.stale = True
16281627

16291628
last_ax = _break_share_link(ax, ax._shared_y_axes)
@@ -1659,7 +1658,7 @@ def clf(self, keep_observers=False):
16591658
self.images = []
16601659
self.legends = []
16611660
if not keep_observers:
1662-
self._axobservers = []
1661+
self._axobservers = cbook.CallbackRegistry()
16631662
self._suptitle = None
16641663
if self.get_constrained_layout():
16651664
layoutbox.nonetree(self._layoutbox)
@@ -1917,10 +1916,9 @@ def gca(self, **kwargs):
19171916
return self.add_subplot(1, 1, 1, **kwargs)
19181917

19191918
def sca(self, a):
1920-
"""Set the current axes to be a and return a."""
1919+
"""Set the current axes to be *a* and return *a*."""
19211920
self._axstack.bubble(a)
1922-
for func in self._axobservers:
1923-
func(self)
1921+
self._axobservers.process("_axes_change_event", self)
19241922
return a
19251923

19261924
def _gci(self):
@@ -1960,12 +1958,10 @@ def _gci(self):
19601958
def __getstate__(self):
19611959
state = super().__getstate__()
19621960

1963-
# the axobservers cannot currently be pickled.
1964-
# Additionally, the canvas cannot currently be pickled, but this has
1965-
# the benefit of meaning that a figure can be detached from one canvas,
1966-
# and re-attached to another.
1967-
for attr_to_pop in ('_axobservers', 'show',
1968-
'canvas', '_cachedRenderer'):
1961+
# The canvas cannot currently be pickled, but this has the benefit
1962+
# of meaning that a figure can be detached from one canvas, and
1963+
# re-attached to another.
1964+
for attr_to_pop in ('show', 'canvas', '_cachedRenderer'):
19691965
state.pop(attr_to_pop, None)
19701966

19711967
# add version information to the state
@@ -1998,7 +1994,6 @@ def __setstate__(self, state):
19981994
self.__dict__ = state
19991995

20001996
# re-initialise some of the unstored state information
2001-
self._axobservers = []
20021997
FigureCanvasBase(self) # Set self.canvas.
20031998
self._layoutbox = None
20041999

@@ -2031,7 +2026,9 @@ def make_active(event):
20312026

20322027
def add_axobserver(self, func):
20332028
"""Whenever the axes state change, ``func(self)`` will be called."""
2034-
self._axobservers.append(func)
2029+
# Connect a wrapper lambda and not func itself, to avoid it being
2030+
# weakref-collected.
2031+
self._axobservers.connect("_axes_change_event", lambda arg: func(arg))
20352032

20362033
def savefig(self, fname, *, transparent=None, **kwargs):
20372034
"""

0 commit comments

Comments
 (0)