From 5c74f1d051341043cc4227f2f8bc7b5108428020 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 1 Nov 2021 22:45:41 +0100 Subject: [PATCH] mark_inset should manually unstale axes limits before drawing itself. --- lib/mpl_toolkits/axes_grid1/inset_locator.py | 20 +++++++++++++++++++- lib/mpl_toolkits/tests/test_axes_grid1.py | 18 +++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/mpl_toolkits/axes_grid1/inset_locator.py b/lib/mpl_toolkits/axes_grid1/inset_locator.py index fcdb32851b8c..a1118d62f89d 100644 --- a/lib/mpl_toolkits/axes_grid1/inset_locator.py +++ b/lib/mpl_toolkits/axes_grid1/inset_locator.py @@ -579,6 +579,22 @@ def zoomed_inset_axes(parent_axes, zoom, loc='upper right', return inset_axes +class _TransformedBboxWithCallback(TransformedBbox): + """ + Variant of `.TransformBbox` which calls *callback* before returning points. + + Used by `.mark_inset` to unstale the parent axes' viewlim as needed. + """ + + def __init__(self, *args, callback, **kwargs): + super().__init__(*args, **kwargs) + self._callback = callback + + def get_points(self): + self._callback() + return super().get_points() + + @docstring.dedent_interpd def mark_inset(parent_axes, inset_axes, loc1, loc2, **kwargs): """ @@ -613,7 +629,9 @@ def mark_inset(parent_axes, inset_axes, loc1, loc2, **kwargs): p1, p2 : `matplotlib.patches.Patch` The patches connecting two corners of the inset axes and its area. """ - rect = TransformedBbox(inset_axes.viewLim, parent_axes.transData) + rect = _TransformedBboxWithCallback( + inset_axes.viewLim, parent_axes.transData, + callback=parent_axes._unstale_viewLim) if 'fill' in kwargs: pp = BboxPatch(rect, **kwargs) diff --git a/lib/mpl_toolkits/tests/test_axes_grid1.py b/lib/mpl_toolkits/tests/test_axes_grid1.py index 3c4aa9a9ebd7..4d4b7f34f439 100644 --- a/lib/mpl_toolkits/tests/test_axes_grid1.py +++ b/lib/mpl_toolkits/tests/test_axes_grid1.py @@ -8,7 +8,7 @@ from matplotlib.colors import LogNorm from matplotlib.transforms import Bbox, TransformedBbox from matplotlib.testing.decorators import ( - image_comparison, remove_ticks_and_titles) + check_figures_equal, image_comparison, remove_ticks_and_titles) from mpl_toolkits.axes_grid1 import ( axes_size as Size, @@ -494,3 +494,19 @@ def test_grid_axes_position(direction): assert loc[1]._nx > loc[0]._nx and loc[2]._ny < loc[0]._ny assert loc[0]._nx == loc[2]._nx and loc[0]._ny == loc[1]._ny assert loc[3]._nx == loc[1]._nx and loc[3]._ny == loc[2]._ny + + +@check_figures_equal(extensions=["png"]) +def test_mark_inset_unstales_viewlim(fig_test, fig_ref): + inset, full = fig_test.subplots(1, 2) + full.plot([0, 5], [0, 5]) + inset.set(xlim=(1, 2), ylim=(1, 2)) + # Check that mark_inset unstales full's viewLim before drawing the marks. + mark_inset(full, inset, 1, 4) + + inset, full = fig_ref.subplots(1, 2) + full.plot([0, 5], [0, 5]) + inset.set(xlim=(1, 2), ylim=(1, 2)) + mark_inset(full, inset, 1, 4) + # Manually unstale the full's viewLim. + fig_ref.canvas.draw()