Skip to content

Add option to gcf/gca to disable new figure/axes creation. #20773

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 1 commit into from

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Jul 31, 2021

This provides a relatively more user-friendly way to check whether a
current figure/axes exists. (_pylab_helpers.Gcf.get_active is way too
"internal".)

Inspired by #20770. I'm not particularly wedded to the kwarg name, better suggestions are welcome.

PR Summary

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

@timhoffm
Copy link
Member

timhoffm commented Aug 1, 2021

Name should probably be something with "create", e.g. "allow_create" or "create_if_none".

@anntzer
Copy link
Contributor Author

anntzer commented Aug 1, 2021

create_if_none it is (for now)

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

At first I was hesitant to touch gca() at all, because it's deeply tied into the pyplot state, and generally I think that's best left in peace.

However, I agree that something has to be done: gca() does two things: 1. obtaining the current axes, 2. creating a new one if needed. While the combination is often what people want in pyplot, it's a shortcoming that we cannot easily check whether a current axes exists.

I'm +/-0 on this solution to the problem. I'm hesitant with a create_if_none kwarg because gca() has a very particular but well established functionality. The create_if_none extension is straight forward, but adds a additional functionality that makes understanding gca() a bit harder. Before the functionality was really only "Get the current Axes, creating one if necessary." Now, we have to make a distinction in case there is no default Axes.

Please consider an explicit plt.has_current_axes() function as an alternative and give your opinion which solution is better.

Side question: For symmetry, should we have a similar behavior for the current figure?

@jklymak
Copy link
Member

jklymak commented Aug 1, 2021

I actually do not understand this PR at all, particularly compared to #20770. The problem in that issue is that we no longer search the stack for matching kwargs to gca(). I don't see what this PR has to do with that?

@anntzer
Copy link
Contributor Author

anntzer commented Aug 1, 2021

@timhoffm plt.has_current_figure() is basically the same as len(plt.get_fignums()) != 0 and plt.has_current_axes() would be len(plt.get_fignums()) and len(gcf().axes); IOW the functionality is basically already there, it's just a question of discoverability. Perhaps we can just add that info to the docstrings instead?

@jklymak I'm specifically adressing the following point in @dstansby's issue:

The only thing I don't know how to get is a list of pyplot axes (pyplot_axes above).

@timhoffm
Copy link
Member

timhoffm commented Aug 1, 2021

I assume that people rarely want to know if there is a current axes (otherwise the request should have come more early/often). So maybe we can get by with documenting the snippet and not introduce any new API. OTOH the question may be fundamental enough to warrant a dedicated API one way or the other.

@jklymak
Copy link
Member

jklymak commented Aug 1, 2021

@jklymak I'm specifically adressing the following point in @dstansby's issue:

The only thing I don't know how to get is a list of pyplot axes (pyplot_axes above).

But thats just fig.axes, or are you concerned they are not explicitly pyplot axes? Does that matter?

@timhoffm
Copy link
Member

timhoffm commented Aug 1, 2021

But thats just fig.axes, or are you concerned they are not explicitly pyplot axes? Does that matter?

The thing is, that from within pyplot, you don't have fig a-priori. You have to obtain it via gcf() but that in turn could create a figure as a side effect. Therefore, one has to do the has_axes = len(plt.get_fignums()) and len(gcf().axes) dance.

@jklymak
Copy link
Member

jklymak commented Aug 1, 2021

Yes, but they want a figure so creating one is fine.

@timhoffm
Copy link
Member

timhoffm commented Aug 2, 2021

Yes, but there should be a side-effect free way of checking if an Axes exists.

Ah, that said: plt.gca() calls gcf().gca(), so as this PR is now plt.gca(create_if_none=False) will still create a Figure if needed. That's quite confusing and makes me 👎 on the proposed impelementation.

@anntzer
Copy link
Contributor Author

anntzer commented Aug 2, 2021

plt.gca() calls gcf().gca(), so as this PR is now plt.gca(create_if_none=False) will still create a Figure if needed.

Good catch, now fixed.

@anntzer
Copy link
Contributor Author

anntzer commented Aug 2, 2021

See #20783 for the "let's just document already existing functionality" approach.

This provides a relatively more user-friendly way to check whether a
current figure/axes exists.  (`_pylab_helpers.Gcf.get_active` is way too
"internal".)
@anntzer
Copy link
Contributor Author

anntzer commented Aug 3, 2021

Closing in favor of #20783 (I think the docs improvement is actually better than expanding the API), though if anyone wants to champion this patch, feel free to pick it up.

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.

3 participants