Skip to content

Commit

Permalink
Speed up liveness check on matplotlib>=3.7.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRikke authored and anntzer committed Aug 20, 2024
1 parent 1d9461c commit 402daf1
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/mplcursors/_mplcursors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import weakref
from weakref import WeakKeyDictionary, WeakSet

import matplotlib as mpl
from matplotlib.axes import Axes
from matplotlib.container import Container
from matplotlib.figure import Figure
Expand Down Expand Up @@ -95,11 +96,15 @@ def _iter_axes_subartists(ax):

def _is_alive(artist):
"""Check whether *artist* is still present on its parent axes."""
return bool(artist
and artist.axes
and (artist.container in artist.axes.containers
if isinstance(artist, _pick_info.ContainerArtist) else
artist in _iter_axes_subartists(artist.axes)))
return bool(
artist
and artist.axes
# `cla()` clears `.axes` since matplotlib/matplotlib#24627 (3.7);
# iterating over subartists can be very slow.
and (getattr(mpl, "__version_info__", ()) >= (3, 7)
or (artist.container in artist.axes.containers
if isinstance(artist, _pick_info.ContainerArtist) else
artist in _iter_axes_subartists(artist.axes))))


def _reassigned_axes_event(event, ax):
Expand Down Expand Up @@ -288,8 +293,6 @@ def __init__(self,
@property
def artists(self):
"""The tuple of selectable artists."""
# Work around matplotlib/matplotlib#6982: `cla()` does not clear
# `.axes`.
return tuple(filter(_is_alive, (ref() for ref in self._artists)))

@property
Expand Down

0 comments on commit 402daf1

Please sign in to comment.