Visitar URL original
Matplotlib v3.9 compatibility by C-Achard · Pull Request #3394 · DeepLabCut/DeepLabCut · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9cc1d18
Add Matplotlib colormap compatibility shim
C-Achard Jul 13, 2026
9518b9d
Allow custom warning stacklevel in deprecated
C-Achard Jul 13, 2026
bd7fdbb
Use shared get_colormap helper everywhere
C-Achard Jul 13, 2026
4bf4921
Merge branch 'main' into cy/init-mpl-version-change
C-Achard Jul 16, 2026
a5d5221
Merge remote-tracking branch 'upstream/main' into cy/init-mpl-version…
deruyter92 Aug 9, 2026
d5945cc
Fix get_colormap_names() legacy fallback to call _legacy_get_colormap…
deruyter92 Aug 9, 2026
d77d4f8
update version for "deprecated since .." warning
deruyter92 Aug 9, 2026
5e739e2
Add name override to deprecated() for legacy shim warnings
deruyter92 Aug 9, 2026
e6cfac6
Rename matplotlib_future_mode.py → matplotlib_compat.py
deruyter92 Aug 9, 2026
d9288fc
add tests for matplotlib compat layer
deruyter92 Aug 9, 2026
350e209
Import matplotlib.cm explicitly in the compat layer
C-Achard Sep 4, 2026
54ac2ac
Prevent duplicate legacy colormap registration
C-Achard Sep 4, 2026
b1c97fb
Route all plt.get_cmap calls throught the compat layer
C-Achard Sep 4, 2026
c1cf140
Use compat helper to clear matplotlib artists
C-Achard Sep 4, 2026
0e4a691
Patch matplotlib's Axes logger by name
C-Achard Sep 4, 2026
d3472c3
Test the name and stacklevel options of @deprecated
C-Achard Sep 4, 2026
0be8837
Expand matplotlib compat test coverage
C-Achard Sep 4, 2026
fe9d488
Remove unused compat helpers
C-Achard Sep 4, 2026
304bcc6
Remove unecessary tests
C-Achard Sep 4, 2026
aa007f6
Rename matplotlib compat module to utils
C-Achard Sep 4, 2026
0584d53
Replace matplotlib_compat colormap helpers
C-Achard Sep 4, 2026
1c15225
Simplify Matplotlib axes logger helper
C-Achard Sep 4, 2026
8a0ea71
Fix Matplotlib artist iteration
C-Achard Sep 4, 2026
426943f
Merge branch 'dev' into cy/init-mpl-version-change
deruyter92 Sep 8, 2026
31380eb
Merge branch 'dev' into cy/init-mpl-version-change
deruyter92 Sep 8, 2026
de29151
Merge branch 'dev' into cy/init-mpl-version-change
deruyter92 Sep 9, 2026
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
Prev Previous commit
Next Next commit
Use compat helper to clear matplotlib artists
Add `remove_artists()` to `matplotlib_compat` and use it in the tracklet toolbox and visualization cleanup paths. This avoids deprecated `ArtistList` concatenation in Matplotlib 3.7+ and preserves safe removal by snapshotting each artist sublist before calling `remove()`.
  • Loading branch information
C-Achard committed Sep 4, 2026
commit c1cf140be038281e35a4c1eb3601266118447aee
6 changes: 3 additions & 3 deletions deeplabcut/gui/tracklet_toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from deeplabcut.refine_training_dataset.tracklets import TrackletManager
from deeplabcut.utils.auxfun_videos import VideoReader
from deeplabcut.utils.auxiliaryfunctions import attempt_to_make_folder
from deeplabcut.utils.matplotlib_compat import get_colormap
from deeplabcut.utils.matplotlib_compat import get_colormap, remove_artists


class DraggablePoint:
Expand Down Expand Up @@ -745,8 +745,8 @@ def on_click(self, event):
self.clean_collections()

def clean_collections(self):
for coll in self.ax2.collections + self.ax3.collections + self.ax_slider.collections:
coll.remove()
for ax in (self.ax2, self.ax3, self.ax_slider):
remove_artists(ax, "collections")

def display_points(self, val):
data = self.manager.xy[:, val]
Expand Down
32 changes: 31 additions & 1 deletion deeplabcut/utils/matplotlib_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@
import matplotlib
import matplotlib.cm # noqa: F401
import matplotlib.pyplot as plt
from matplotlib.axes import Axes
from matplotlib.colors import Colormap

from deeplabcut.core.deprecation import deprecated

DLC_MATPLOTLIB_LEGACY_DEPRECATED_SINCE = "3.0.2"
DLC_MATPLOTLIB_LEGACY_DEPRECATED_SINCE = (
"3.0.2" # TODO: @C-Achard move this to the centralized deprecations declarations before merging
)

# Deliberately not every sublist an ``Axes`` exposes
# ``texts``, ``tables`` and ``containers`` are omitted
# because no DeepLabCut caller used them, and these five reproduce the
# behaviour of the removal loops this helper replaced
ARTIST_KINDS = ("lines", "collections", "artists", "patches", "images")


def get_colormap(
Expand Down Expand Up @@ -65,6 +74,27 @@ def unregister_colormap(name: str) -> None:
_legacy_unregister_colormap(name)


def remove_artists(ax: Axes, *kinds: str) -> None:
"""Remove artists from an ``Axes``, one children sublist at a time.

Matplotlib 3.7 made the ``Axes`` children sublists (``ax.lines``,
``ax.collections``, ...) immutable ``ArtistList`` views and deprecated
concatenating them with ``+``. Iterating a concatenation also happened to
be the only thing making the removal loops safe, since ``+`` returned a
plain list and therefore a snapshot; iterating a live ``ArtistList`` while
calling ``Artist.remove()`` skips artists. This helper snapshots each
sublist explicitly instead.

Args:
ax: The axes to remove artists from.
*kinds: Names of the children sublists to clear, e.g. ``"collections"``.
Defaults to every kind in ``ARTIST_KINDS``.
"""
for kind in kinds or ARTIST_KINDS:
for artist in list(getattr(ax, kind)):
artist.remove()


def _get_legacy_colormap_names() -> list[str]:
"""Return registered colormap names without emitting a DLC warning."""
return list(plt.colormaps())
Expand Down
5 changes: 2 additions & 3 deletions deeplabcut/utils/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from tqdm import trange

from deeplabcut.utils import auxfun_videos, auxiliaryfunctions
from deeplabcut.utils.matplotlib_compat import get_colormap
from deeplabcut.utils.matplotlib_compat import get_colormap, remove_artists


def get_cmap(n: int, name: str = "hsv") -> Colormap:
Expand Down Expand Up @@ -286,8 +286,7 @@ def create_minimal_figure(dpi=100):


def erase_artists(ax):
for artist in ax.lines + ax.collections + ax.artists + ax.patches + ax.images:
artist.remove()
remove_artists(ax)
ax.figure.canvas.draw_idle()


Expand Down