Skip to content

Commit 3d99e43

Browse files
authored
Merge pull request #8043 from pavoljuhas/fix-pyplot-axes-in-other-figure
Fix pyplot.axis(ax) when ax is in other figure.
2 parents 96c75be + 2fa64ad commit 3d99e43

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/matplotlib/pyplot.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,8 @@ def axes(*args, **kwargs):
875875
color for the axis, default white.
876876
877877
- ``axes(h)`` where *h* is an axes instance makes *h* the current
878-
axis. An :class:`~matplotlib.axes.Axes` instance is returned.
878+
axis and the parent of *h* the current figure.
879+
An :class:`~matplotlib.axes.Axes` instance is returned.
879880
880881
========= ============== ==============================================
881882
kwarg Accepts Description
@@ -909,7 +910,8 @@ def axes(*args, **kwargs):
909910
arg = args[0]
910911

911912
if isinstance(arg, Axes):
912-
a = gcf().sca(arg)
913+
sca(arg)
914+
a = arg
913915
else:
914916
rect = arg
915917
a = gcf().add_axes(rect, **kwargs)

lib/matplotlib/tests/test_axes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,17 @@ def _as_mpl_axes(self):
14771477
plt.close()
14781478

14791479

1480+
def test_pyplot_axes():
1481+
# test focusing of Axes in other Figure
1482+
fig1, ax1 = plt.subplots()
1483+
fig2, ax2 = plt.subplots()
1484+
assert ax1 is plt.axes(ax1)
1485+
assert ax1 is plt.gca()
1486+
assert fig1 is plt.gcf()
1487+
plt.close(fig1)
1488+
plt.close(fig2)
1489+
1490+
14801491
@image_comparison(baseline_images=['log_scales'])
14811492
def test_log_scales():
14821493
fig = plt.figure()

0 commit comments

Comments
 (0)