-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Conversation
Name should probably be something with "create", e.g. "allow_create" or "create_if_none". |
create_if_none it is (for now) |
There was a problem hiding this 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?
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? |
@timhoffm @jklymak I'm specifically adressing the following point in @dstansby's issue:
|
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. |
The thing is, that from within pyplot, you don't have |
Yes, but they want a figure so creating one is fine. |
Yes, but there should be a side-effect free way of checking if an Axes exists. Ah, that said: |
Good catch, now fixed. |
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".)
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. |
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
pytest
passes).flake8
on changed files to check).flake8-docstrings
and runflake8 --docstring-convention=all
).doc/users/next_whats_new/
(follow instructions in README.rst there).doc/api/next_api_changes/
(follow instructions in README.rst there).