From d572823610e7a954175462e591f6068ea65381d8 Mon Sep 17 00:00:00 2001 From: cvanelteren Date: Mon, 27 Jan 2025 10:05:02 +0100 Subject: [PATCH 1/7] fixed inset zoom for 3.10 --- ultraplot/axes/base.py | 74 +++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/ultraplot/axes/base.py b/ultraplot/axes/base.py index 26bb984f1..e9b41b885 100644 --- a/ultraplot/axes/base.py +++ b/ultraplot/axes/base.py @@ -2881,35 +2881,71 @@ def indicate_inset_zoom(self, **kwargs): """ %(axes.indicate_inset)s """ + import matplotlib as mpl + from packaging import version + # Add the inset indicators parent = self._inset_parent if not parent: raise ValueError("This command can only be called from an inset axes.") + kwargs.update(_pop_props(kwargs, "patch")) # impose alternative defaults + if not self._inset_zoom_artists: kwargs.setdefault("zorder", 3.5) kwargs.setdefault("linewidth", rc["axes.linewidth"]) kwargs.setdefault("edgecolor", rc["axes.edgecolor"]) + xlim, ylim = self.get_xlim(), self.get_ylim() - rect = (xlim[0], ylim[0], xlim[1] - xlim[0], ylim[1] - ylim[0]) - rectpatch, connects = parent.indicate_inset(rect, self) - - # Update indicator properties - # NOTE: Unlike matplotlib we sync zoom box properties with connection lines. - if self._inset_zoom_artists: - rectpatch_prev, connects_prev = self._inset_zoom_artists - rectpatch.update_from(rectpatch_prev) - rectpatch.set_zorder(rectpatch_prev.get_zorder()) - rectpatch_prev.remove() - for line, line_prev in zip(connects, connects_prev): - line.update_from(line_prev) - line.set_zorder(line_prev.get_zorder()) # not included in update_from - line_prev.remove() - rectpatch.update(kwargs) - for line in connects: - line.update(kwargs) - self._inset_zoom_artists = (rectpatch, connects) - return rectpatch, connects + bounds = (xlim[0], ylim[0], xlim[1] - xlim[0], ylim[1] - ylim[0]) + + if version.parse(mpl.__version__) >= version.parse("3.10"): + # Implementation for matplotlib >= 3.10 + self.apply_aspect() + kwargs.setdefault("label", "_indicate_inset") + if kwargs.get("transform") is None: + kwargs["transform"] = self.transData + + from matplotlib.inset import InsetIndicator + + indicator = InsetIndicator( + bounds=bounds, inset_ax=self, facecolor="none", **kwargs + ) + + if self._inset_zoom_artists: + indicator = self._inset_zoom_artists + indicator.update(kwargs) + indicator.rectangle.update(kwargs) + [c.update(kwargs) for c in indicator.connectors] + else: + self._inset_zoom_artists = indicator + self.add_artist(indicator) + + return (indicator.rectangle, indicator.connectors) + + else: + # Implementation for matplotlib < 3.10 + rectpatch, connects = parent.indicate_inset(bounds, self) + + # Update indicator properties + if self._inset_zoom_artists: + rectpatch_prev, connects_prev = self._inset_zoom_artists + rectpatch.update_from(rectpatch_prev) + rectpatch.set_zorder(rectpatch_prev.get_zorder()) + rectpatch_prev.remove() + for line, line_prev in zip(connects, connects_prev): + line.update_from(line_prev) + line.set_zorder( + line_prev.get_zorder() + ) # not included in update_from + line_prev.remove() + + rectpatch.update(kwargs) + for line in connects: + line.update(kwargs) + + self._inset_zoom_artists = (rectpatch, connects) + return rectpatch, connects @docstring._snippet_manager def panel(self, side=None, **kwargs): From 8b2e0e2d3e7bee3cb65cdcc0fa51cb725fecfc9f Mon Sep 17 00:00:00 2001 From: cvanelteren Date: Mon, 27 Jan 2025 10:11:47 +0100 Subject: [PATCH 2/7] add deprecation warning for 3.12 onwards --- ultraplot/axes/base.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ultraplot/axes/base.py b/ultraplot/axes/base.py index e9b41b885..c025dba02 100644 --- a/ultraplot/axes/base.py +++ b/ultraplot/axes/base.py @@ -2901,6 +2901,12 @@ def indicate_inset_zoom(self, **kwargs): if version.parse(mpl.__version__) >= version.parse("3.10"): # Implementation for matplotlib >= 3.10 + # TODO: Remove this block when support for matplotlib < 3.10 is dropped + if version.parse(mpl.__version__) >= version.parse("3.12"): + warnings._warn_ultraplot( + "Support for matplotlib < 3.10 will be removed in a future version. " + "Please upgrade to matplotlib >= 3.10.", + ) self.apply_aspect() kwargs.setdefault("label", "_indicate_inset") if kwargs.get("transform") is None: From 6b70d4b289233e3e76ca6000696220426f70f65f Mon Sep 17 00:00:00 2001 From: cvanelteren Date: Mon, 27 Jan 2025 10:14:23 +0100 Subject: [PATCH 3/7] added comments --- ultraplot/axes/base.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ultraplot/axes/base.py b/ultraplot/axes/base.py index c025dba02..7f38e03fe 100644 --- a/ultraplot/axes/base.py +++ b/ultraplot/axes/base.py @@ -2914,6 +2914,7 @@ def indicate_inset_zoom(self, **kwargs): from matplotlib.inset import InsetIndicator + # NOTE: Explicitly set facecolor here to none, as it seems to be ignore -- not sure why as it is the default indicator = InsetIndicator( bounds=bounds, inset_ax=self, facecolor="none", **kwargs ) From 1ca3ffe4764a31bea9036fbe1075a3dded4711a3 Mon Sep 17 00:00:00 2001 From: cvanelteren Date: Mon, 27 Jan 2025 13:37:25 +0100 Subject: [PATCH 4/7] set facecolor default to none if does not exist --- ultraplot/axes/base.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ultraplot/axes/base.py b/ultraplot/axes/base.py index 7f38e03fe..d4589fd58 100644 --- a/ultraplot/axes/base.py +++ b/ultraplot/axes/base.py @@ -2914,10 +2914,9 @@ def indicate_inset_zoom(self, **kwargs): from matplotlib.inset import InsetIndicator - # NOTE: Explicitly set facecolor here to none, as it seems to be ignore -- not sure why as it is the default + kwargs.setdefault("facecolor", "none") indicator = InsetIndicator( - bounds=bounds, inset_ax=self, facecolor="none", **kwargs - ) + bounds=bounds, inset_ax=self, **kwargs) if self._inset_zoom_artists: indicator = self._inset_zoom_artists From c11ac8bc34ab3de6350d76aedfab6cbcae5eda5e Mon Sep 17 00:00:00 2001 From: cvanelteren Date: Mon, 27 Jan 2025 14:59:21 +0100 Subject: [PATCH 5/7] remove facecolor - not necessary --- ultraplot/axes/base.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ultraplot/axes/base.py b/ultraplot/axes/base.py index d4589fd58..cbd18692e 100644 --- a/ultraplot/axes/base.py +++ b/ultraplot/axes/base.py @@ -2914,7 +2914,6 @@ def indicate_inset_zoom(self, **kwargs): from matplotlib.inset import InsetIndicator - kwargs.setdefault("facecolor", "none") indicator = InsetIndicator( bounds=bounds, inset_ax=self, **kwargs) From 14d370c79418938212405d5025d2fc93babdf57a Mon Sep 17 00:00:00 2001 From: cvanelteren Date: Mon, 27 Jan 2025 18:16:13 +0100 Subject: [PATCH 6/7] black formatting --- ultraplot/axes/base.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ultraplot/axes/base.py b/ultraplot/axes/base.py index cbd18692e..c4ae59ec9 100644 --- a/ultraplot/axes/base.py +++ b/ultraplot/axes/base.py @@ -2904,9 +2904,9 @@ def indicate_inset_zoom(self, **kwargs): # TODO: Remove this block when support for matplotlib < 3.10 is dropped if version.parse(mpl.__version__) >= version.parse("3.12"): warnings._warn_ultraplot( - "Support for matplotlib < 3.10 will be removed in a future version. " - "Please upgrade to matplotlib >= 3.10.", - ) + "Support for matplotlib < 3.10 will be removed in a future version. " + "Please upgrade to matplotlib >= 3.10.", + ) self.apply_aspect() kwargs.setdefault("label", "_indicate_inset") if kwargs.get("transform") is None: @@ -2914,8 +2914,7 @@ def indicate_inset_zoom(self, **kwargs): from matplotlib.inset import InsetIndicator - indicator = InsetIndicator( - bounds=bounds, inset_ax=self, **kwargs) + indicator = InsetIndicator(bounds=bounds, inset_ax=self, **kwargs) if self._inset_zoom_artists: indicator = self._inset_zoom_artists From cc635063eebcf23221bce006d2ebcf7e1bccab45 Mon Sep 17 00:00:00 2001 From: cvanelteren Date: Mon, 27 Jan 2025 18:24:38 +0100 Subject: [PATCH 7/7] removed dep. msg and added context --- ultraplot/axes/base.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ultraplot/axes/base.py b/ultraplot/axes/base.py index c4ae59ec9..f84d602e9 100644 --- a/ultraplot/axes/base.py +++ b/ultraplot/axes/base.py @@ -2901,12 +2901,7 @@ def indicate_inset_zoom(self, **kwargs): if version.parse(mpl.__version__) >= version.parse("3.10"): # Implementation for matplotlib >= 3.10 - # TODO: Remove this block when support for matplotlib < 3.10 is dropped - if version.parse(mpl.__version__) >= version.parse("3.12"): - warnings._warn_ultraplot( - "Support for matplotlib < 3.10 will be removed in a future version. " - "Please upgrade to matplotlib >= 3.10.", - ) + # 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. self.apply_aspect() kwargs.setdefault("label", "_indicate_inset") if kwargs.get("transform") is None: