-
Notifications
You must be signed in to change notification settings - Fork 224
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
make %matplotlib widget
behave like inline
?
#171
Comments
One of the great things about With I suggest that you use fig, ax = plt.subplots() and then use the methods on Depending on what you are using |
@tacaswell , thank you for your answer! I realize you guys have made up your mind, but I cannot resist stating that Of course |
I agree it is different, but not inherently more complicated. At the cost of being slightly more verbose you get much better clarity about what is going on (and maybe a net reduction in typing as A switch that works today is to put If you want to orphan a figure from pyplot's internal registry when it is displayed, that is probably possible (put the logic in the display logic), however for panning / zooming we need to make sure that the figure does not get garbage collected (so that we can re-draw it on pan/zoom). If you are holding a reference to it that is ok, but if not (you did Having typed this out, I think I see a path to this which is to add a "quasi-orphan" option to pyplot removes a figure from the |
Thanks again. I hadn't thought about it in that detail and now understand better what the challenge is. Better, but not completely. Wouldn't it be enough if at the start of each cell, matplotlib acted as if there was no current figure (yet), analogous to the situation after startup, so that any pyplot statement creates a new figure? That doesn't necessarily mean that figures are orphaned from the registry, only that the existence of one or more figures in the registry doesn't imply
Wouldn't it be possible that the notebook output cell holds that reference? I.e. the notebook itself plays the role of the "second shadow registry"? – In the view of the role of figures in a notebook that I outlined, Not sure whether any of this makes sense... |
@tacaswell, me again. I tried the workaround you suggested, putting Some of the figures are controlled by an
If I run through all cells and then revisit earlier ones, these remain interactive in the sense that I can zoom and pan, but the slider doesn't do anything anymore. If instead I put I'm afraid I still don't know what's happening. I understand that in each subsequent cell I overwrite |
Sorry for the very long delay, I started to write this and then it got lost in my too many open tabs...
It is my understanding that with the execution model of jupyter things on the python side do not know anything about the cell. It is a feature that Matplotlib can not event tell if it is being executed in IPython or in a notebook, and certainly not where the cell boundaries are. There are however hooks that Jupyter calls on the way out (and maybe on the way in?) that could do some of this management. That is not how scoping works in python, all of the callbacks are walking up to find the global scope and finding the ax from the last cell you executed.
should work because it will bind the See https://stackoverflow.com/questions/13355233/python-lambda-closure-scoping for a long explanation as to why. |
When you select a figure it makes it the "active" figure so |
@tacaswell, thanks once more! You're right, I realized that I had a wrong idea of Python closures. I assumed that if a function references a global object, and that object goes away, the function becomes a closure, but that is not the case. I also found that I'm sorry to say that despite your extensive and appreciated efforts to help I've since decided to use Plotly. I believe what makes Plotly easier to use within a notebook is that figures are values, which appear in the output if they are the result of the last evaluation in a cell (or are |
With ipympl there is a bunch of javascript bound to the output cell, there is just a mirror back to the python side as well. Both bokeh and plotly only have js displays. While this does have some major upsides (like the user interaction happens in the browser, not via a round-trip communication with the kernel), you do give up things as well (like being able to move back out of the browser, the whole set of file formats Matplotlib saves to, etc). |
I've been thinking more and more about this issue - the fundamental tension is that this problem makes it very hard for users to mostly work with inline figures, but only occasionally switch to widget ones. Unless they rewrite a lot of the code in a given notebook, chances are it won't work the way they expect it... There's a side effect from matplotlib's success that is a bit problematic: it's common these days to end up with mpl figures that were auto-created by e.g. pandas or xarray plot calls... It's a larger update to ask the users to always move their pandas code to manual It's impossible purely in Python to tell the difference between the user keeping a figure open deliberately or re-running a cell (and thus ending up with "useless" figures open inside of We might also be able to inject automatic figure creation logic... This would definitely change the experience, and it would interfere with more advance uses where users deliberately modify an open figure from different cells. But something like a Before trying to implement anything specific, curious what folks here think? |
I'm 💯 on (fully) closing any I think what user want is to have
The global state in If you could wait for a mpl release cycle, it is also worth thinking about adding a "selectable" state to the While, this proposal would interfere with someone doing I have also been thinking about if we can re-thing the way Gcf works all together (see https://github.com/tacaswell/mpl-gui), but have not been able to put any time into it in the last ~7 months :( |
I have a notebook in Jupyter Lab, so far using the default matplotlib backend, which in this context I believe is
inline
. When I have a sequence of plotting commands (pyplot.plt
etc.), they combine into a figure that is shown after the respective code cell. Also,@interact
works as expected.I now wanted to zoom in, which
inline
does not support, so I issued%matplotlib widget
. The figures are now zoomable alright, but I don't have that neat behavior where plotting code in one cell contributed to the following figure output cell, and doesn't modify anything else. And@interact
, instead of producing new (versions of) a figure, now puts everything on top of each other.From reading around I realize that this is the intended behavior. The thing is, I don't like it. So my question is: Is there a way to make
widget
behave likeinline
?If there is not, is there a guide that tells me how to modify my code so that plot commands affect only the next output cell, and
@interact
updates instead of adds?The text was updated successfully, but these errors were encountered: