diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index c085fb405b9a..f6801ff4ca66 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -875,7 +875,8 @@ def axes(*args, **kwargs): color for the axis, default white. - ``axes(h)`` where *h* is an axes instance makes *h* the current - axis. An :class:`~matplotlib.axes.Axes` instance is returned. + axis and the parent of *h* the current figure. + An :class:`~matplotlib.axes.Axes` instance is returned. ========= ============== ============================================== kwarg Accepts Description @@ -909,7 +910,8 @@ def axes(*args, **kwargs): arg = args[0] if isinstance(arg, Axes): - a = gcf().sca(arg) + sca(arg) + a = arg else: rect = arg a = gcf().add_axes(rect, **kwargs) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 19cc9267b794..40a107b252aa 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1477,6 +1477,17 @@ def _as_mpl_axes(self): plt.close() +def test_pyplot_axes(): + # test focusing of Axes in other Figure + fig1, ax1 = plt.subplots() + fig2, ax2 = plt.subplots() + assert ax1 is plt.axes(ax1) + assert ax1 is plt.gca() + assert fig1 is plt.gcf() + plt.close(fig1) + plt.close(fig2) + + @image_comparison(baseline_images=['log_scales']) def test_log_scales(): fig = plt.figure()