Skip to content

Commit 1161c69

Browse files
authored
Compatiblity update for mpl 3.10 (#33)
- Adds version-specific implementation for matplotlib ≥3.10 using the new InsetIndicator class - Maintains existing implementation for matplotlib <3.10 using rectangle and patches - Both implementations share the same interface and return format
1 parent 1382ceb commit 1161c69

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

ultraplot/axes/base.py

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2881,35 +2881,70 @@ def indicate_inset_zoom(self, **kwargs):
28812881
"""
28822882
%(axes.indicate_inset)s
28832883
"""
2884+
import matplotlib as mpl
2885+
from packaging import version
2886+
28842887
# Add the inset indicators
28852888
parent = self._inset_parent
28862889
if not parent:
28872890
raise ValueError("This command can only be called from an inset axes.")
2891+
28882892
kwargs.update(_pop_props(kwargs, "patch")) # impose alternative defaults
2893+
28892894
if not self._inset_zoom_artists:
28902895
kwargs.setdefault("zorder", 3.5)
28912896
kwargs.setdefault("linewidth", rc["axes.linewidth"])
28922897
kwargs.setdefault("edgecolor", rc["axes.edgecolor"])
2898+
28932899
xlim, ylim = self.get_xlim(), self.get_ylim()
2894-
rect = (xlim[0], ylim[0], xlim[1] - xlim[0], ylim[1] - ylim[0])
2895-
rectpatch, connects = parent.indicate_inset(rect, self)
2896-
2897-
# Update indicator properties
2898-
# NOTE: Unlike matplotlib we sync zoom box properties with connection lines.
2899-
if self._inset_zoom_artists:
2900-
rectpatch_prev, connects_prev = self._inset_zoom_artists
2901-
rectpatch.update_from(rectpatch_prev)
2902-
rectpatch.set_zorder(rectpatch_prev.get_zorder())
2903-
rectpatch_prev.remove()
2904-
for line, line_prev in zip(connects, connects_prev):
2905-
line.update_from(line_prev)
2906-
line.set_zorder(line_prev.get_zorder()) # not included in update_from
2907-
line_prev.remove()
2908-
rectpatch.update(kwargs)
2909-
for line in connects:
2910-
line.update(kwargs)
2911-
self._inset_zoom_artists = (rectpatch, connects)
2912-
return rectpatch, connects
2900+
bounds = (xlim[0], ylim[0], xlim[1] - xlim[0], ylim[1] - ylim[0])
2901+
2902+
if version.parse(mpl.__version__) >= version.parse("3.10"):
2903+
# Implementation for matplotlib >= 3.10
2904+
# NOTE: if the api changes we need to deprecate the old one. At the time of writing the IndicateInset is experimental and may change in the future. This would require us to change potentially the return signature of this function.
2905+
self.apply_aspect()
2906+
kwargs.setdefault("label", "_indicate_inset")
2907+
if kwargs.get("transform") is None:
2908+
kwargs["transform"] = self.transData
2909+
2910+
from matplotlib.inset import InsetIndicator
2911+
2912+
indicator = InsetIndicator(bounds=bounds, inset_ax=self, **kwargs)
2913+
2914+
if self._inset_zoom_artists:
2915+
indicator = self._inset_zoom_artists
2916+
indicator.update(kwargs)
2917+
indicator.rectangle.update(kwargs)
2918+
[c.update(kwargs) for c in indicator.connectors]
2919+
else:
2920+
self._inset_zoom_artists = indicator
2921+
self.add_artist(indicator)
2922+
2923+
return (indicator.rectangle, indicator.connectors)
2924+
2925+
else:
2926+
# Implementation for matplotlib < 3.10
2927+
rectpatch, connects = parent.indicate_inset(bounds, self)
2928+
2929+
# Update indicator properties
2930+
if self._inset_zoom_artists:
2931+
rectpatch_prev, connects_prev = self._inset_zoom_artists
2932+
rectpatch.update_from(rectpatch_prev)
2933+
rectpatch.set_zorder(rectpatch_prev.get_zorder())
2934+
rectpatch_prev.remove()
2935+
for line, line_prev in zip(connects, connects_prev):
2936+
line.update_from(line_prev)
2937+
line.set_zorder(
2938+
line_prev.get_zorder()
2939+
) # not included in update_from
2940+
line_prev.remove()
2941+
2942+
rectpatch.update(kwargs)
2943+
for line in connects:
2944+
line.update(kwargs)
2945+
2946+
self._inset_zoom_artists = (rectpatch, connects)
2947+
return rectpatch, connects
29132948

29142949
@docstring._snippet_manager
29152950
def panel(self, side=None, **kwargs):

0 commit comments

Comments
 (0)