Skip to content

[Doc]: How to replicate behaviour of plt.gca(projection=...)? #20770

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
dstansby opened this issue Jul 30, 2021 · 5 comments
Closed

[Doc]: How to replicate behaviour of plt.gca(projection=...)? #20770

dstansby opened this issue Jul 30, 2021 · 5 comments
Milestone

Comments

@dstansby
Copy link
Member

Documentation Link

https://matplotlib.org/stable/users/whats_new.html#changes-to-behavior-of-axes-creation-methods-gca-add-axes-add-subplot

Problem

In version 3.4, passing keyword arguments to plt.gca() was deprecated. I was previously using plt.gca(projection='polar') to:

  1. Check if a polar projection axes exists, and if so return it
  2. If not, create a new axes with a polar projection

Looking at the documentation (https://matplotlib.org/stable/users/whats_new.html#changes-to-behavior-of-axes-creation-methods-gca-add-axes-add-subplot) it's not clear to me how to replicate this behaviour however. In pseudo-code I think what I want to do is:

if len(pyplot_axes):
    if plt.gca().name != 'polar':
        raise RuntimeError('Axes isn't polar projection')
    else:
        return plt.gca()
else:
    return plt.subplot(projection='polar')

The only thing I don't know how to get is a list of pyplot axes (pyplot_axes above). I looked at https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.html and didn't see anything, but perhaps there's another page I'm missing?

Suggested improvement

Add a clear description to the changelog (https://matplotlib.org/stable/users/whats_new.html#changes-to-behavior-of-axes-creation-methods-gca-add-axes-add-subplot) showing how to write code to reproduce plt.gca(projection=...).

Matplotlib Version

3.4.2

Matplotlib documentation version

3.4.2

@anntzer
Copy link
Contributor

anntzer commented Jul 31, 2021

I guess you are looking for is something like if plt.get_fignums() and plt.gcf().axes? (get_fignums() will tell you if there's any pyplot figures around, and if there's one around you want to check if they already have any axes)

@jklymak
Copy link
Member

jklymak commented Jul 31, 2021

Do we remember when this came in. I'm pretty sure we discussed this issue.

@jklymak
Copy link
Member

jklymak commented Aug 1, 2021

OK, this was #19153 and had a pretty long deprecation message, if I understand correctly. Was the deprecation message not showing up for plt.gca()?

@dsantsby, can you explain more your use case here? I guess I can see that one might organize one's axes this way, but it seems very ad-hoc to create a new polar axes unless one already exists, and in that case use the existing axes. Over all we are really trying to move away from state-based thinking, and this is a pretty extreme example.

@dstansby
Copy link
Member Author

dstansby commented Aug 1, 2021

The deprecation message is showing up for plt.gca(), but it doesn't explain how to replicate the behaviour of plt.gca(projection=...). I've copied the message at the bottom of this comment.

My use case is a plot method in another library, which when called with no specified Axes I want to:

  1. Get the current axes
  2. If these are polar, use these axes
  3. If not, raise an error
  4. If no current axes are present, create a new polar axes

The reason I can't use plt.gca() for step one is it will create a new Axes with a non-polar projection, and it's not then possible to change the projection after creating the Axes. #20773 seems to fix this.

Over all we are really trying to move away from state-based thinking, and this is a pretty extreme example.

This is fair, but there's nothing in the what's new changelog entry explaining this or discouraging users. So I think there should either be a clear explanation to this effect explaining why plt.gca(projection=...) is discouraged, or some clear instructions on how to replicate the behaviour of plt.gca(projection=...).

matplotlib._api.deprecation.MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().

@anntzer
Copy link
Contributor

anntzer commented Sep 2, 2021

@dstansby I'll close this, as #20773 was superseded by #20783 which should(?) be sufficient and has been merged, but feel free to reopen or PR more docs if you disagree.

@anntzer anntzer closed this as completed Sep 2, 2021
@QuLogic QuLogic added this to the v3.5.0 milestone Sep 2, 2021
jarrodmillman added a commit to jarrodmillman/scientific-python-lectures that referenced this issue Sep 16, 2022
pdebuyl pushed a commit to pdebuyl/scipy-lecture-notes that referenced this issue Sep 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants