-
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
Plot disappears after executing cell second time #60
Comments
This is because I'm not sure what the "correct" behaviour should be. I feel that notebook users can be tricked by the plot call producing a figure, when they shouldn't be expecting The solution is to explicitly call |
Indeed this appears to be the expected behavior. I think that a better way to use this is to turn off interacrive mode with |
Agreed. That is how "normal" matplotlib users do it :) |
I don't see how this is the expected behaviour since it differs from the behaviour in every other backend. If I use I see that if I call This takes me to another point of using the |
The reason the behaviour is different from qt, is that in qt the plot window does not appear in the output of the cell. The output is reset each time the cell is called. This isn't the case for qt, since it appears in a separate window. For the case of I guess what you would want/expect, is for the figure to appear in some place on the screen where it will not be hidden from view by further calls to that cell. |
I "fix" this behavior by always calling |
I've had the same issue switching from jupyter to jupyter lab and going from %matplotlib inline or notebook to %matplotlib widget. For me, I just do: plt.close('chart_name') Then it redraws it for me. The solution by thomasaarholt worked for me as well, but I prefer to not have all my plots in a separate cells |
Since the temporary solution of @bshinnebarger is still somewhat cumbersome, I tried to simplify the matter even further. You can make a template notebook with the help of this extension and add the following line to your template using %matplotlib ipympl
def figure(name, *args, **kwargs):
plt.close(name)
plt.figure(name, *args, **kwargs)
import pylab as plt Afterwards, in any cell that contains a plot, you can simply use figure('name') without the namespace |
@tiagopereira I would argue that this is an issue with the display model of jupyter notebooks rather than Matplotlib. @thomasaarholt got the mechanism exactly right. Rather than manually closing the figures manually etc, I suggest fig, ax = plt.subplots()
ax.plot(...) instead. This create a new figure every run and by using the OO interface you can be sure what plot you will be putting things into. This future proofs you against wanting to split plotting across more than one cell, wanting to add sub-plots, moving to jupyterlab, and pulling your plotting out into a function. |
@thomasaarholt @tacaswell this does not close the old (hidden) figure and can create a huge number of figures that stay open. Using the same name indicates that I want to recreate a figure, not make a new one. I would expect that if a figure's output widget is removed, then the figure is closed automatically. Like closing the window. I do not know the full mechanism of how widgets are rendered, but one would hope there is some event/hook to respond when a widget is removed from the output cell. My preference would be that re-evaluating a cell should cause the display-widget to close the figure. |
@jfemiani If we are leaking invisible figures I agree that is a bug! |
I am using Seaborn to plot data with ipympl 0.8.2 Expected behavior should be similar to |
If I have a plot in a notebook cell, and execute it twice, the plot output disappears. This is not what is expected. For example, using this code in one jupyter notebook cell:
and this in another cell:
The first time I run this, everything plots fine. But when I execute the second cell twice (or any following calls), the plot disappears. The expected behaviour is that it re-plots (using a different colour). If I then re-run the first cell (with
%matplotlib widget
), and then the second cell again, it works (but this basically just resets the plot).If I include a plot command in the first cell, then I can re-run the second cell as many times as I want and it works as expected.
This does not happen if I use
%matplotlib inline
. I've tested in both jupyter lab and jupyter notebook. My versions are the following:The text was updated successfully, but these errors were encountered: