Fix pyplot.axis(ax) when ax is in other figure. #8043

Merged
merged 4 commits into from Feb 11, 2017

Conversation

Projects
None yet
6 participants
Contributor

pavoljuhas commented Feb 7, 2017

Avoid crash when ax belongs to some other than the current figure.
Make the ax owner the current figure instead.

This resolves crash in axes call in the following stanza

from matplotlib.pyplot import *
f1, a1 = subplots()
f2, a2 = subplots()
axes(a1)
assert gca() is a1
assert gcf() is f1
@pavoljuhas pavoljuhas 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.
37d7f1f
Contributor

dopplershift commented Feb 8, 2017

Is there any chance you could add a test in test_axes.py that replicates the crash? It could be as simple as:

def test_set_axes_different_figure():
    f1, a1 = plt.subplots()
    f2, a2 = plt.subplots()
    plt.axes(a1)
    assert plt.gca() is a1
    assert plt.gcf() is f1

This way we don't accidentally break it in the future.

Contributor

pavoljuhas commented Feb 8, 2017

Sure, I'll get to it later today or tomorrow.

@pavoljuhas pavoljuhas Test pyplot.axes(ax) with ax from other figure.
Ensure `ax` becomes the current axis and its parent the current figure.
712116b

tacaswell added this to the 2.1 (next point release) milestone Feb 8, 2017

@NelleV

NelleV approved these changes Feb 10, 2017

LGTM
Thanks!

NelleV changed the title from Fix pyplot.axis(ax) when ax is in other figure. to [MRG+1] Fix pyplot.axis(ax) when ax is in other figure. Feb 10, 2017

Contributor

dstansby commented Feb 10, 2017 edited

Thanks for the fix! 🤦‍♂️ I should check the tests before I approve something next time.

@dstansby

Just a couple of test fixes needed.

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()
@dstansby

dstansby Feb 10, 2017

Contributor

Could the new lines go in a new function?

@pavoljuhas

pavoljuhas Feb 10, 2017

Contributor

Indeed test_as_mpl_axes_api seems to be for customizations with the _as_mpl_axes function. I will add the new function.

lib/matplotlib/tests/test_axes.py
+ # test focusing of Axes in other Figure
+ fig1, ax1 = subplots()
+ fig2, ax2 = subplots()
+ assert ax1 is plt.axes(ax1)
@dstansby

dstansby Feb 10, 2017 edited

Contributor

These two lines need to be plt.subplots()

@QuLogic

QuLogic Feb 10, 2017

Member

This comment is one line below where it should be.

@dstansby

dstansby Feb 10, 2017

Contributor

Woops, thanks for spotting that

@pavoljuhas

pavoljuhas Feb 10, 2017

Contributor

Fixed - thanks for catching that.

pavoljuhas added some commits Feb 10, 2017

@pavoljuhas pavoljuhas Add missing module to plt.subplots call.
383e256
@pavoljuhas pavoljuhas Move pyplot.axes test to a dedicated function.
2fa64ad

@NelleV NelleV merged commit 3d99e43 into matplotlib:master Feb 11, 2017

5 checks passed

codecov/patch 100% of diff hit (target 80%)
Details
codecov/project/library 62.28% (+<.01%) compared to c15694b
Details
codecov/project/tests 97.97% (target 97.7%)
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

pavoljuhas deleted the pavoljuhas:fix-pyplot-axes-in-other-figure branch Feb 11, 2017

QuLogic removed the needs_review label Feb 12, 2017

QuLogic changed the title from [MRG+1] Fix pyplot.axis(ax) when ax is in other figure. to Fix pyplot.axis(ax) when ax is in other figure. Feb 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment