8000 Inset orientation (#14686) · matplotlib/matplotlib@2bc6721 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2bc6721

Browse files
authored
Inset orientation (#14686)
Inset orientation
2 parents 633a83c + 6eef5e1 commit 2bc6721

File tree

2 files changed

+66
-40
lines changed

2 files changed

+66
-40
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -423,13 +423,12 @@ def inset_axes(self, bounds, *, transform=None, zorder=5,
423423
parent axes.
424424
425425
**kwargs
426-
427-
Other *kwargs* are passed on to the `axes.Axes` child axes.
426+
Other *kwargs* are passed on to the `~.axes.Axes` child axes.
428427
429428
Returns
430429
-------
431-
Axes
432-
The created `.axes.Axes` instance.
430+
ax
431+
The created `~.axes.Axes` instance.
433432
434433
Examples
435434
--------
@@ -468,8 +467,7 @@ def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
468467
"""
469468
Add an inset indicator to the axes. This is a rectangle on the plot
470469
at the position indicated by *bounds* that optionally has lines that
471-
connect the rectangle to an inset axes
472-
(`.Axes.inset_axes`).
470+
connect the rectangle to an inset axes (`.Axes.inset_axes`).
473471
474472
Warnings
475473
--------
@@ -499,10 +497,10 @@ def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
499497
Color of the rectangle and color of the connecting lines. Default
500498
is '0.5'.
501499
502-
alpha : number
500+
alpha : float
503501
Transparency of the rectangle and connector lines. Default is 0.5.
504502
505-
zorder : number
503+
zorder : float
506504
Drawing order of the rectangle and connector lines. Default is 4.99
507505
(just below the default level of inset axes).
508506
@@ -511,19 +509,16 @@ def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
511509
512510
Returns
513511
-------
514-
rectangle_patch : `.Patches.Rectangle`
515-
Rectangle artist.
512+
rectangle_patch : `.patches.Rectangle`
513+
The indicator frame.
516514
517-
connector_lines : optional 4-tuple of `.Patches.ConnectionPatch`
518-
Each of four connector lines coming from the given rectangle
519-
on this axes in the order lower left, upper left, lower right,
520-
upper right: *None* if *inset_ax* is *None*.
521-
Two are set with visibility to *False*,
522-
but the user can set the visibility to *True* if the
523-
automatic choice is not deemed correct.
515+
connector_lines : 4-tuple of `.patches.ConnectionPatch`
516+
The four connector lines connecting to (lower_left, upper_left,
517+
lower_right upper_right) corners of *inset_ax*. Two lines are
518+
set with visibility to *False*, but the user can set the
519+
visibility to True if the automatic choice is not deemed correct.
524520
525521
"""
526-
527522
# to make the axes connectors work, we need to apply the aspect to
528523
# the parent axes.
529524
self.apply_aspect()
@@ -532,31 +527,36 @@ def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
532527
transform = self.transData
533528
label = kwargs.pop('label', 'indicate_inset')
534529

535-
xy = (bounds[0], bounds[1])
536-
rectpatch = mpatches.Rectangle(xy, bounds[2], bounds[3],
537-
facecolor=facecolor, edgecolor=edgecolor, alpha=alpha,
538-
zorder=zorder, label=label, transform=transform, **kwargs)
539-
self.add_patch(rectpatch)
530+
x, y, width, height = bounds
531+
rectangle_patch = mpatches.Rectangle(
532+
(x, y), width, height,
533+
facecolor=facecolor, edgecolor=edgecolor, alpha=alpha,
534+
zorder=zorder, label=label, transform=transform, **kwargs)
535+
self.add_patch(rectangle_patch)
540536

541537
connects = []
542538

543539
if inset_ax is not None:
544-
# want to connect the indicator to the rect....
545-
xr = [bounds[0], bounds[0]+bounds[2]]
546-
yr = [bounds[1], bounds[1]+bounds[3]]
547-
for xc in range(2):
548-
for yc in range(2):
549-
xyA = (xc, yc)
550-
xyB = (xr[xc], yr[yc])
551-
connects.append(
552-
mpatches.ConnectionPatch(
553-
xyA, xyB,
554-
'axes fraction', 'data',
555-
axesA=inset_ax, axesB=self, arrowstyle="-",
556-
zorder=zorder, edgecolor=edgecolor, alpha=alpha
557-
)
558-
)
559-
self.add_patch(connects[-1])
540+
# connect the inset_axes to the rectangle
541+
for xy_inset_ax in [(0, 0), (0, 1), (1, 0), (1, 1)]:
542+
# inset_ax positions are in axes coordinates
543+
# The 0, 1 values define the four edges if the inset_ax
544+
# lower_left, upper_left, lower_right upper_right.
545+
ex, ey = xy_inset_ax
546+
if self.xaxis.get_inverted():
547+
ex = 1 - ex
548+
if self.yaxis.get_inverted():
549+
ey = 1 - ey
550+
xy_data = x + ex * width, y + ey * height
551+
p = mpatches.ConnectionPatch(xy_inset_ax, xy_data,
552+
coordsA='axes fraction',
553+
coordsB='data',
554+
axesA=inset_ax, axesB=self,
555+
arrowstyle="-", zorder=zorder,
556+
edgecolor=edgecolor, alpha=alpha)
557+
connects.append(p)
558+
self.add_patch(p)
559+
560560
# decide which two of the lines to keep visible....
561561
pos = inset_ax.get_position()
562562
bboxins = pos.transformed(self.figure.transFigure)
@@ -572,7 +572,7 @@ def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
572572
connects[2].set_visible(x1 == y0)
573573
connects[3].set_visible(x1 ^ y1)
574574

575-
return rectpatch, tuple(connects) if connects else None
575+
return rectangle_patch, tuple(connects) if connects else None
576576

577577
def indicate_inset_zoom(self, inset_ax, **kwargs):
578578
"""

lib/matplotlib/tests/test_axes.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6021,6 +6021,32 @@ def test_zoom_inset():
60216021
xx, rtol=1e-4)
60226022

60236023

6024+
@pytest.mark.parametrize('x_inverted', [False, True])
6025+
@pytest.mark.parametrize('y_inverted', [False, True])
6026+
def test_indicate_inset_inverted(x_inverted, y_inverted):
6027+
"""
6028+
Test that the inset lines are correctly located with inverted data axes.
6029+
"""
6030+
fig, (ax1, ax2) = plt.subplots(1, 2)
6031+
6032+
x = np.arange(10)
6033+
ax1.plot(x, x, 'o')
6034+
if x_inverted:
6035+
ax1.invert_xaxis()
6036+
if y_inverted:
6037+
ax1.invert_yaxis()
6038+
6039+
rect, bounds = ax1.indicate_inset([2, 2, 5, 4], ax2)
6040+
lower_left, upper_left, lower_right, upper_right = bounds
6041+
6042+
sign_x = -1 if x_inverted else 1
6043+
sign_y = -1 if y_inverted else 1
6044+
assert sign_x * (lower_right.xy2[0] - lower_left.xy2[0]) > 0
6045+
assert sign_x * (upper_right.xy2[0] - upper_left.xy2[0]) > 0
6046+
assert sign_y * (upper_left.xy2[1] - lower_left.xy2[1]) > 0
6047+
assert sign_y * (upper_right.xy2[1] - lower_right.xy2[1]) > 0
6048+
6049+
60246050
def test_set_position():
60256051
fig, ax = plt.subplots()
60266052
ax.set_aspect(3.)

0 commit comments

Comments
 (0)
0