-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
add context manager functionality to ion and ioff #17371
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
@tacaswell @QuLogic @efiring I don't understand |
Seems fine, but it's still draft... |
I rebased and started trying to build the docs in order to see how my change affected the docs. I followed the instructions here (https://matplotlib.org/3.1.1/devel/documenting_mpl.html) and even tried them in a fresh conda env:
But I keep running into this mystifying error: Any advice for fixing that error/discovering what the error is? It makes it much harder to work out the above issues with the docs. Though I can confirm from the build artifact that the ioff doc page no longer exists. |
@QuLogic sorry I didn't see your comment before I posted the above. The code changes work for daily usage (I have started using this by monkeypatching it in and using daily) but the remaining issue is that by making |
this enables usage of plt.ioff as a context manager to allow creating figures without the figure automatically displaying in environments such as jupyter. Usage: with plt.ioff(): plt.figure()
need to ensure that the original state is restored post contextmanager
I just rewrote this context manager in the same manner as the |
... The "Ready For Review" button doesn't work? Happy to click it for you but just wanted to see what the interface is? |
🤦 I was looking at the top right section of the page - totally the wrong place. Sorry about that and thanks for the tip |
NP, just didn't want to convert things to draft if the author couldn't un-draft it..... |
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.
Does this work recursively?
with plt.ion():
with plt.ion():
pass
or
with plt.ion():
with plt.ioff():
pass
or even
with plt.ioff():
with plt.ion():
with plt.ioff():
pass
The tests all failed due to a primordial implementation of |
Co-Authored-By: Tim Hoffmann <timhoffm@users.noreply.github.com>
I added more information to docstrings. Thinking more about the previously proposed An alternative to adding extra documentation and then hoping that users don't do this we could create a separate As an aside: What is difference between single and double backticks as used in matplotlib docstrings? This SO question seems to imply that the meaning of single backticks can vary project to project. |
This is reStructuredText; single are cross-references, double are code blocks. |
For cross-references see also https://matplotlib.org/devdocs/devel/documenting_mpl.html#referring-to-other-code. Let me think a little about returning a context manager vs. a separate function. |
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.
IMHO it is better to have a function with a (potentially unused) return value context object than creating additional functions ioff/ion_context()
.
One strong argument is that standardlib open()
also does not have two functions.
The suggestions try to make the situation still a little bit more clear.
@timhoffm my initial solution avoided the issue of the return value by making I've updated the documentation with your helpful clarifications, and I made the examples in |
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.
What about testing a nested context?
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
I added variations on nested contexts all of which work. I did think of one more pathological case. What is the expected behavior for: plt.ioff()
with plt.ioff():
plt.ion()
print(mpl.is_interactive()) currently the context manager will leave you in interactive mode at the end It should be easy to change it so that interactive mode is off in the end, but I'm 50/50 on what's best. |
I think the context manager should change state on entry into the block, and restore the previous state on exit. Therefore IMO at the end of your code interactive mode should be off. |
PR Summary
Closes: #17013
Also closes: matplotlib/ipympl#220
This allows the usage of
plt.ioff
andplt.ion
as context managers:It also caches the state and returns to the original state on exit:
Documentation - WIP - advice welcome
I think that this additional behavior ought to be documented but I was a bit unsure how to go about it. In general things in the documentation seem divided into being functions or objects, and this PR puts
ion
andioff
somewhere between. So I think it makes sense to keep these on this page: https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.html?highlight=pyplot#module-matplotlib.pyplot. But, then they would be mislabelled as Functions, and I'm not sure that they will end up there anyway as that page is autogenerated. I also wasn't sure which docstring to put information about the context manager usage in, I think the most sensible place is in the__call__
method, but this isn't strictly what is used as the context manager.The documentation needs be updated at these links:
https://matplotlib.org/3.2.1/users/shell.html#controlling-interactive-updating
https://matplotlib.org/3.2.1/tutorials/introductory/usage.html?highlight=usage#what-is-interactive-mode
I'll give this a go but wanted to get input on where to put extended docstrings and how to manage the autogenerating docs. Also, if anyone has any wording suggestions I'd be happy to hear them.
Tests
There don't seem to be any tests that make use of
ion
orioff
returns no results. Does this PR warrant adding new tests?
PR Checklist