make `%matplotlib widget` behave like `inline`?
See original GitHub issueI 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 like inline
?
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?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:11 (7 by maintainers)
@tacaswell , thank you for your answer!
I realize you guys have made up your mind, but I cannot resist stating that
<speech>
I think in the context of a notebook, a global current figure doesn’t make sense. The idea of a notebook, as I understand it, is to go step by step through a computation and to visualize results along the way. A notebook is to be read like a text, and in a text what comes after follows from what comes before, not the other way around. An exception would be LaTeX-style figure floats, but that doesn’t exist in a notebook. Now I can make my code more complicated to work around that, but that again in my mind goes against the spirit of a notebook; the code should be as simple as possible, for graphics focusing on what is shown and how, not fiddling around with internals.</speech>
Of course
pyplot
is built around the concept of a current figure, there’s no way around it. Butinline
is based on matplotlib, too, so wouldn’t it be possible to have a switch / option / configuration setting that enables the same behavior inwidget
?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
ax
(at function definition time) to the local nameax_inner
.See https://stackoverflow.com/questions/13355233/python-lambda-closure-scoping for a long explanation as to why.