Thanks to visit codestin.com
Credit goes to github.com

Skip to content

indicate_inset transform support #29174

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
Dec 20, 2024
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
2 changes: 1 addition & 1 deletion lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def indicate_inset(self, bounds=None, inset_ax=None, *, transform=None,

transform : `.Transform`
Transform for the rectangle coordinates. Defaults to
``ax.transAxes``, i.e. the units of *rect* are in Axes-relative
``ax.transData``, i.e. the units of *rect* are in the Axes' data
coordinates.

facecolor : :mpltype:`color`, default: 'none'
Expand Down
11 changes: 9 additions & 2 deletions lib/matplotlib/inset.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def _shared_setter(self, prop, val):

artist.setp([self._rectangle, *self._connectors], prop, val)

@artist.Artist.axes.setter
def axes(self, new_axes):
# Set axes on the rectangle (required for some external transforms to work) as
# well as the InsetIndicator artist.
self.rectangle.axes = new_axes
artist.Artist.axes.fset(self, new_axes)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the setter logic here from the artist within artist example. I would not have figured it out by myself - thanks @QuLogic for putting it in the example!


def set_alpha(self, alpha):
# docstring inherited
self._shared_setter('alpha', alpha)
Expand Down Expand Up @@ -171,7 +178,7 @@ def _update_connectors(self):
# parent artist.
p = ConnectionPatch(
xyA=xy_inset_ax, coordsA=self._inset_ax.transAxes,
xyB=xy_data, coordsB=self.axes.transData,
xyB=xy_data, coordsB=self.rectangle.get_data_transform(),
arrowstyle="-",
edgecolor=self._edgecolor, alpha=self.get_alpha(),
linestyle=self._linestyle, linewidth=self._linewidth)
Expand All @@ -182,7 +189,7 @@ def _update_connectors(self):
existing.xy1 = xy_inset_ax
existing.xy2 = xy_data
existing.coords1 = self._inset_ax.transAxes
existing.coords2 = self.axes.transData
existing.coords2 = self.rectangle.get_data_transform()

if existing is None:
# decide which two of the lines to keep visible....
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions lib/matplotlib/tests/test_inset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
from matplotlib.testing.decorators import image_comparison, check_figures_equal


Expand Down Expand Up @@ -104,3 +105,38 @@
# Make one visible connector a different style
indicator.connectors[1].set_linestyle('dashed')
indicator.connectors[1].set_color('blue')


@image_comparison(['zoom_inset_transform.png'], remove_text=True, style='mpl20',
tol=0.01)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MacOS tests need the 0.01 tolerance.

def test_zoom_inset_transform():
fig, ax = plt.subplots()

ax_ins = ax.inset_axes([0.2, 0.2, 0.3, 0.15])
ax_ins.set_ylim([0.3, 0.6])
ax_ins.set_xlim([0.5, 0.9])

tr = mtransforms.Affine2D().rotate_deg(30)
indicator = ax.indicate_inset_zoom(ax_ins, transform=tr + ax.transData)
for conn in indicator.connectors:
conn.set_visible(True)


def test_zoom_inset_external_transform():
# Smoke test that an external transform that requires an axes (i.e.
# Cartopy) will work.
class FussyDataTr:
def _as_mpl_transform(self, axes=None):
if axes is None:
raise ValueError("I am a fussy transform that requires an axes")

Check warning on line 131 in lib/matplotlib/tests/test_inset.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/tests/test_inset.py#L131

Added line #L131 was not covered by tests
return axes.transData

fig, ax = plt.subplots()

ax_ins = ax.inset_axes([0.2, 0.2, 0.3, 0.15])
ax_ins.set_xlim([0.7, 0.8])
ax_ins.set_ylim([0.7, 0.8])

ax.indicate_inset_zoom(ax_ins, transform=FussyDataTr())

fig.draw_without_rendering()
Loading