From 37d7f1f8a25bfac529922faac56532e8fa2c87c3 Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Tue, 7 Feb 2017 18:38:37 -0500 Subject: [PATCH 1/4] Fix pyplot.axes(ax) when ax is in other figure. Avoid crash when `ax` belongs to some other than the current figure. Make the `ax` owner the current figure instead. --- lib/matplotlib/pyplot.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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) From 712116ba6ccd663089019e7312eb677d0e843484 Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Wed, 8 Feb 2017 11:43:09 -0500 Subject: [PATCH 2/4] Test pyplot.axes(ax) with ax from other figure. Ensure `ax` becomes the current axis and its parent the current figure. --- lib/matplotlib/tests/test_axes.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 19cc9267b794..32beb8935167 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1476,6 +1476,15 @@ def _as_mpl_axes(self): 'Expected a PolarAxesSubplot, got %s' % type(ax) plt.close() + # test focusing of Axes in other Figure + fig1, ax1 = subplots() + fig2, ax2 = 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(): From 383e256a0cada55bd326f6b15a8b0db9e6b2d66f Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Fri, 10 Feb 2017 18:29:32 -0500 Subject: [PATCH 3/4] Add missing module to plt.subplots call. --- lib/matplotlib/tests/test_axes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 32beb8935167..035342b8a3b8 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1477,8 +1477,8 @@ def _as_mpl_axes(self): plt.close() # test focusing of Axes in other Figure - fig1, ax1 = subplots() - fig2, ax2 = subplots() + fig1, ax1 = plt.subplots() + fig2, ax2 = plt.subplots() assert ax1 is plt.axes(ax1) assert ax1 is plt.gca() assert fig1 is plt.gcf() From 2fa64ad6623df39f42c2c5ee263a2062ab0e10ba Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Fri, 10 Feb 2017 18:51:51 -0500 Subject: [PATCH 4/4] Move pyplot.axes test to a dedicated function. --- lib/matplotlib/tests/test_axes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 035342b8a3b8..40a107b252aa 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1476,6 +1476,8 @@ def _as_mpl_axes(self): 'Expected a PolarAxesSubplot, got %s' % type(ax) plt.close() + +def test_pyplot_axes(): # test focusing of Axes in other Figure fig1, ax1 = plt.subplots() fig2, ax2 = plt.subplots()