Skip to content

Commit a3682f3

Browse files
authored
Merge pull request matplotlib#29174 from rcomer/inset-conn-transform
`indicate_inset` transform support
2 parents 82adc45 + 95ed490 commit a3682f3

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

lib/matplotlib/axes/_axes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def indicate_inset(self, bounds=None, inset_ax=None, *, transform=None,
440440
441441
transform : `.Transform`
442442
Transform for the rectangle coordinates. Defaults to
443-
``ax.transAxes``, i.e. the units of *rect* are in Axes-relative
443+
``ax.transData``, i.e. the units of *rect* are in the Axes' data
444444
coordinates.
445445
446446
facecolor : :mpltype:`color`, default: 'none'

lib/matplotlib/inset.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ def _shared_setter(self, prop, val):
7979

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

82+
@artist.Artist.axes.setter
83+
def axes(self, new_axes):
84+
# Set axes on the rectangle (required for some external transforms to work) as
85+
# well as the InsetIndicator artist.
86+
self.rectangle.axes = new_axes
87+
artist.Artist.axes.fset(self, new_axes)
88+
8289
def set_alpha(self, alpha):
8390
# docstring inherited
8491
self._shared_setter('alpha', alpha)
@@ -171,7 +178,7 @@ def _update_connectors(self):
171178
# parent artist.
172179
p = ConnectionPatch(
173180
xyA=xy_inset_ax, coordsA=self._inset_ax.transAxes,
174-
xyB=xy_data, coordsB=self.axes.transData,
181+
xyB=xy_data, coordsB=self.rectangle.get_data_transform(),
175182
arrowstyle="-",
176183
edgecolor=self._edgecolor, alpha=self.get_alpha(),
177184
linestyle=self._linestyle, linewidth=self._linewidth)
@@ -182,7 +189,7 @@ def _update_connectors(self):
182189
existing.xy1 = xy_inset_ax
183190
existing.xy2 = xy_data
184191
existing.coords1 = self._inset_ax.transAxes
185-
existing.coords2 = self.axes.transData
192+
existing.coords2 = self.rectangle.get_data_transform()
186193

187194
if existing is None:
188195
# decide which two of the lines to keep visible....
Loading

lib/matplotlib/tests/test_inset.py

+36
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import matplotlib.colors as mcolors
66
import matplotlib.pyplot as plt
7+
import matplotlib.transforms as mtransforms
78
from matplotlib.testing.decorators import image_comparison, check_figures_equal
89

910

@@ -104,3 +105,38 @@ def test_zoom_inset_connector_styles():
104105
# Make one visible connector a different style
105106
indicator.connectors[1].set_linestyle('dashed')
106107
indicator.connectors[1].set_color('blue')
108+
109+
110+
@image_comparison(['zoom_inset_transform.png'], remove_text=True, style='mpl20',
111+
tol=0.01)
112+
def test_zoom_inset_transform():
113+
fig, ax = plt.subplots()
114+
115+
ax_ins = ax.inset_axes([0.2, 0.2, 0.3, 0.15])
116+
ax_ins.set_ylim([0.3, 0.6])
117+
ax_ins.set_xlim([0.5, 0.9])
118+
119+
tr = mtransforms.Affine2D().rotate_deg(30)
120+
indicator = ax.indicate_inset_zoom(ax_ins, transform=tr + ax.transData)
121+
for conn in indicator.connectors:
122+
conn.set_visible(True)
123+
124+
125+
def test_zoom_inset_external_transform():
126+
# Smoke test that an external transform that requires an axes (i.e.
127+
# Cartopy) will work.
128+
class FussyDataTr:
129+
def _as_mpl_transform(self, axes=None):
130+
if axes is None:
131+
raise ValueError("I am a fussy transform that requires an axes")
132+
return axes.transData
133+
134+
fig, ax = plt.subplots()
135+
136+
ax_ins = ax.inset_axes([0.2, 0.2, 0.3, 0.15])
137+
ax_ins.set_xlim([0.7, 0.8])
138+
ax_ins.set_ylim([0.7, 0.8])
139+
140+
ax.indicate_inset_zoom(ax_ins, transform=FussyDataTr())
141+
142+
fig.draw_without_rendering()

0 commit comments

Comments
 (0)