Skip to content

mark_inset should manually unstale axes limits before drawing itself. #21519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 19 additions & 1 deletion lib/mpl_toolkits/axes_grid1/inset_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 17 additions & 1 deletion lib/mpl_toolkits/tests/test_axes_grid1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()