Skip to content

Fix getting polar axes in plt.polar() #10674

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1822,11 +1822,12 @@ def gca(self, **kwargs):
"3.0",
"Calling `gca()` using the same arguments as a "
"previous axes currently reuses the earlier "
"instance. In a future version, a new instance will "
"always be created and returned. Meanwhile, this "
"warning can be suppressed, and the future behavior "
"ensured, by passing a unique label to each axes "
"instance.")
"instance.\n"
Copy link
Member Author

Choose a reason for hiding this comment

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

Also added some newlines here to make the warning more readable.

"In a future version, a new instance will "
"always be created and returned.\n"
"Meanwhile, this warning can be suppressed, and the "
"future behavior ensured, by passing a unique label "
"to each axes instance.")
return cax
else:
warnings.warn('Requested projection is different from '
Expand Down
7 changes: 5 additions & 2 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2283,12 +2283,15 @@ def polar(*args, **kwargs):
strings, as in :func:`~matplotlib.pyplot.plot`.

"""
# If an axis already exists, check if it has a polar projection
# If an axes already exists, check if it has a polar projection
if gcf().get_axes():
if not isinstance(gca(), PolarAxes):
warnings.warn('Trying to create polar plot on an axis that does '
'not have a polar projection.')
ax = gca(polar=True)
ax = gca()
else:
ax = gcf().add_subplot(1, 1, 1, polar=True)
Copy link
Member

Choose a reason for hiding this comment

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

umm, I'm not too up on the pyplot interface, but is gca() necessarilly a subplot(111)?

Copy link
Member

Choose a reason for hiding this comment

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

No, but: If there are axes, use the gca(). If not, make a new one equivalent to subplot(111).

Copy link
Member

Choose a reason for hiding this comment

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

OK, but I don't get the point of this PR then. Why is this better than whats there now? Is the idea to let plt.polar write on an existing polar axes, despite the fact gca will create a new axes in the future? That doesn't seem right. Sorry if I'm being dense...

Copy link
Member

Choose a reason for hiding this comment

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

The whole gcasituation is incredibly confusing. I can't make sense of the current behavior and docstrings, and the warning about future behavior makes no sense to me either. gca means "get current axes"; it should not mean "always get a new axes". I think that the coming change to subplot is being incorrectly applied to gca.

Copy link
Member Author

Choose a reason for hiding this comment

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

The change was made in #10660 - I think gca should just get the current axis, and not create a new one, and that change should be reverted. ping @anntzer

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that the warning in #10660 is wrong. I need to rethink about the correct way to ultimately get rid of the axes collision behavior.

Copy link
Member Author

Choose a reason for hiding this comment

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

Great, I'll close this then.


ret = ax.plot(*args, **kwargs)
return ret

Expand Down