Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 11, 2017

Conversation

pavoljuhas
Copy link
Contributor

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

Avoid crash when `ax` belongs to some other than the current figure.
Make the `ax` owner the current figure instead.
@pavoljuhas pavoljuhas force-pushed the fix-pyplot-axes-in-other-figure branch from 9422ba3 to 37d7f1f Compare February 7, 2017 23:56
@dopplershift
Copy link
Contributor

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.

@pavoljuhas
Copy link
Contributor Author

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

Ensure `ax` becomes the current axis and its parent the current figure.
@tacaswell tacaswell added this to the 2.1 (next point release) milestone Feb 8, 2017
Copy link
Member

@NelleV NelleV left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Thanks!

@NelleV NelleV changed the title Fix pyplot.axis(ax) when ax is in other figure. [MRG+1] Fix pyplot.axis(ax) when ax is in other figure. Feb 10, 2017
@dstansby
Copy link
Member

dstansby commented Feb 10, 2017

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

Copy link
Member

@dstansby dstansby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of test fixes needed.

# test focusing of Axes in other Figure
fig1, ax1 = subplots()
fig2, ax2 = subplots()
assert ax1 is plt.axes(ax1)
Copy link
Member

@dstansby dstansby Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines need to be plt.subplots()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is one line below where it should be.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops, thanks for spotting that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed - thanks for catching that.

@@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the new lines go in a new function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@NelleV NelleV merged commit 3d99e43 into matplotlib:master Feb 11, 2017
@pavoljuhas pavoljuhas deleted the fix-pyplot-axes-in-other-figure branch February 11, 2017 18:13
@QuLogic QuLogic changed the title [MRG+1] Fix pyplot.axis(ax) when ax is in other figure. 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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants