-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: plt.interactive(False) not preventing figure generation in jupyter unless %matplotlib inline
is run first
#23766
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
Comments
The non-interactive mode is mainly targeted at command line shells where it is cumbersome to write multi-line commands. I don't know whether anybody has looked into that topic for the inline backend, which is tied to notebooks and thus makes it inherently easy to write code blocks into one cell and execute them together. That said, what your motivation to spread the figure creation across multiple cells? In general I would not recommend this approach because it's more brittle - the figure will likely only work if you call all related cells in order. |
My motivation is that I want to create my figure, do stuff to it in cells below (e.g. add lines, or set the axis range), and only when my figure is ready I want to display it. I want to stop Matplotlib from automatically displaying it in the notebook as soon as it's created. |
You'll be running against the inline backend concepts here. At least you have to deactivate auto-closing of figures at the end of cells https://github.com/ipython/matplotlib-inline/blob/fbf0ab8f2d81993ff8e319a003f58896c77f2443/matplotlib_inline/backend_inline.py#L289 There are likely more pitfalls here, but I'm not an expert for the inline backend. I still would suggest to separate data manipulation and plotting:
This should make your code much clearer than spreading the figure code across multiple cells. |
Issue with automatically displaying figures: I have my own class p = Plot(<some input>)
p this will currently display the figure twice, once from the auto-displaying and once from the I want a p = Plot(<some input>)
p.ylim(10, 20)
p (this would also display two figures). I might also want to make multiple figures, and then place them inside widget containers, arranging them as I want, e.g. p1 = Plot(<some input1>)
p2 = Plot(<some input2>)
from ipywidgets import ipw
ipw.HBox([p1, p2]) which now would display first the two matplotlib figures from Basically I just want to be able to control when the figure is displayed, which I can if I run |
Notebooks in the inline backend are not interactive. If @nvaytet, if you really want different behaviour here for an important reason, then the appropriate place to report is https://github.com/ipython/matplotlib-inline. Thanks! |
@jklymak my question wasn't specifically about the |
Probably your best place to discuss is https://discourse.matplotlib.org. Widgets are discussed at https://github.com/matplotlib/ipympl and inline https://github.com/ipython/matplotlib-inline.
Again, this is better asked at https://github.com/ipython/matplotlib-inline. |
One final remark / recommendation: @nvaytet you are wrapping Matplotlib both on the API side as well as the output side. In that case, you're most likely better off using the 'agg' backend internally to write image data and use this image in your output. In your case |
@timhoffm thanks for the comment. But my users still want to have plots that they can zoom/pan onto in their notebooks. So I cannot avoid using the |
Ok, but then we're not talking about the |
Bug summary
Using
plt.interactive(False)
orplt.ioff()
in Jupyter (Lab and classic notebook) does not seem to prevent automatic figure generation, unless the magic%matplotlib inline
in run first, even though theinline
backend is the default.Code for reproduction
Actual outcome
Expected outcome
Explicitly running

%matplotlib inline
block figure creation:Note that running

%matplotlib inline
has to be done BEFORE the first figure was created, if not it has no effectAdditional information
Using the
widget
backend works in the same way asinline
: if called before any figures are created, figures are not automatically generated. If called after one figure was made, thenplt.interactive(False)
has no effect.I also tried
plt.ioff()
instead ofplt.interactive(False)
but it gives the same results.Operating system
Ubuntu 22.04
Matplotlib Version
3.5.2
Matplotlib Backend
inline (jupyterlab)
Python version
3.9
Jupyter version
3.4.2
Installation
conda
The text was updated successfully, but these errors were encountered: