Skip to content

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

Closed

Description

@dstansby

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

Activity

  1. anntzer commented on Jul 31, 2021

    @anntzer
    Contributor

    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)

  2. jklymak commented on Jul 31, 2021

    @jklymak
    Member

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

  3. jklymak commented on Aug 1, 2021

    @jklymak
    Member

    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.

  4. dstansby commented on Aug 1, 2021

    @dstansby
    MemberAuthor

    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().

  5. anntzer commented on Sep 2, 2021

    @anntzer
    Contributor

    @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.

  6. added this to the v3.5.0 milestone on Sep 2, 2021
  7. added a commit that references this issue on Sep 16, 2022
  8. added a commit that references this issue on Sep 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions