question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

make `%matplotlib widget` behave like `inline`?

See original GitHub issue

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 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:open
  • Created 4 years ago
  • Reactions:5
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
allefeldcommented, Feb 2, 2020

@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. But inline is based on matplotlib, too, so wouldn’t it be possible to have a switch / option / configuration setting that enables the same behavior in widget?

1reaction
tacaswellcommented, Feb 20, 2020

Sorry for the very long delay, I started to write this and then it got lost in my too many open tabs…

Wouldn’t it be enough if at the start of each cell, matplotlib acted as if there was no current figure (yet)

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.

fig, ax = plt.subplots()
@interact(alpha=(0.01, 1.0, 0.01))
def isotonicPlot(alpha = 0.3, *, ax_inner=ax):
    ax_innner.clear()
    ax_inner.plot(xs, FsIsotonic(xs, alpha), Xu, FsNaive(Xu, alpha), 'bo')

should work because it will bind the ax (at function definition time) to the local name ax_inner.

See https://stackoverflow.com/questions/13355233/python-lambda-closure-scoping for a long explanation as to why.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make matplotlib button widget actuate when pressed, not ...
This button behaves as intended, but actuates when the mouse button is released, and not when it is pressed. If I click on...
Read more >
Interactive figures — Matplotlib 3.6.2 documentation
The default backend in notebooks, the inline backend, is not. backend_inline renders the figure once and inserts a static image into the notebook...
Read more >
A Qt Console for IPython
This is a very lightweight widget that largely feels like a terminal, but provides a number of enhancements only possible in a GUI,...
Read more >
Interactive data analysis with figurewidget ipywidgets in ...
import datetime import numpy as np import pandas as pd import plotly.graph_objects as go from ipywidgets import widgets. We'll be making an application...
Read more >
Jupyterlab Inline Interactive plot
You either need to create a new figure before plotting by plt.figure() , or you need to use subplots. Otherwise, plt.show() will act...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found