8000 Fix locating the connector lines in inverted axes · matplotlib/matplotlib@d60459d · GitHub
[go: up one dir, main page]

Skip to content

Commit d60459d

Browse files
committed
Fix locating the connector lines in inverted axes
1 parent ce72670 commit d60459d

File tree

2 files changed

+66
-33
lines changed

2 files changed

+66
-33
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 40 additions & 33 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 rectan 8000 gle 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,16 +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 : 4-tuple of `.Patches.ConnectionPatch`
518-
One for each of four connector lines. Two are set with visibility
519-
to *False*, but the user can set the visibility to True if the
520-
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.
521520
522521
"""
523-
524522
# to make the axes connectors work, we need to apply the aspect to
525523
# the parent axes.
526524
self.apply_aspect()
@@ -529,26 +527,35 @@ def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
529527
transform = self.transData
530528
label = kwargs.pop('label', 'indicate_inset')
531529

532-
xy = (bounds[0], bounds[1])
533-
rectpatch = mpatches.Rectangle(xy, bounds[2], bounds[3],
534-
facecolor=facecolor, edgecolor=edgecolor, alpha=alpha,
535-
zorder=zorder, label=label, transform=transform, **kwargs)
536-
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)
537536

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

566-
return rectpatch, connects
573+
return rectangle_patch, connects
567574

568575
def indicate_inset_zoom(self, inset_ax, **kwargs):
569576
"""

lib/matplotlib/tests/test_axes.py

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

59935993

5994+
@pytest.mark.parametrize('x_inverted', [False, True])
5995+
@pytest.mark.parametrize('y_inverted', [False, True])
5996+
def test_indicate_inset_inverted(x_inverted, y_inverted):
5997+
"""
5998+
Test that the inset lines are correctly located with inverted data axes.
5999+
"""
6000+
fig, (ax1, ax2) = plt.subplots(1, 2)
6001+
6002+
x = np.arange(10)
6003+
ax1.plot(x, x, 'o')
6004+
if x_inverted:
6005+
ax1.invert_xaxis()
6006+
if y_inverted:
6007+
ax1.invert_yaxis()
6008+
6009+
rect, bounds = ax1.indicate_inset([2, 2, 5, 4], ax2)
6010+
lower_left, upper_left, lower_right, upper_right = bounds
6011+
6012+
sign_x = -1 if x_inverted else 1
6013+
sign_y = -1 if y_inverted else 1
6014+
assert sign_x * (lower_right.xy2[0] - lower_left.xy2[0]) > 0
6015+
assert sign_x * (upper_right.xy2[0] - upper_left.xy2[0]) > 0
6016+
assert sign_y * (upper_left.xy2[1] - lower_left.xy2[1]) > 0
6017+
assert sign_y * (upper_right.xy2[1] - lower_right.xy2[1]) > 0
6018+
6019+
59946020
def test_set_position():
59956021
fig, ax = plt.subplots()
59966022
ax.set_aspect(3.)

0 commit comments

Comments
 (0)
0