Plot disappears after executing cell second time
See original GitHub issueIf 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:
%matplotlib widget
import matplotlib.pyplot as plt
and this in another cell:
x = [0, 1, 2, 3]
y = [0, 1, 2, 3]
plt.plot(x, y)
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:
matplotlib 2.2.2 py36_1 conda-forge
ipympl 0.2.1 py36_0 conda-forge
@jupyter-widgets/jupyterlab-manager
@jupyter-widgets/jupyterlab-manager v0.35.0 enabled OK
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:13 (7 by maintainers)

Top Related StackOverflow Question
@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
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.
I don’t see how this is the expected behaviour since it differs from the behaviour in every other backend. If I use
inline, a new plot/figure is created every time I re-run the cell. If I useqt, the original figure is updated every time I re-run. Withwidget, the original figure disappears and nothing gets plotted in the second and subsequent runs. It is not logical that a call toplotwill erase an existing figure and plot nothing new.I see that if I call
plt.figure()in the first cell, the behaviour is indeed what is expected. But why does thewidgetbackend require this additional step when no other backend does?This takes me to another point of using the
widgetbackend. I’ve taught a course using jupyter and thewidgetbackend, and the top complaint from students was figures updating in a different cell / forgetting to callplt.figure(). I’ve used matplotlib for a long time and understand the architecture of figure/axes/plot. However, for notebook usage this should be rethought. Many jupyter uses make use ofinline, and this will create a new figure in the output of every cell that calls plotting functions. This makes sense coming from Mathematica. Withwidget, however, it creates a figure in the output of the first cell that has plotting calls, but absent a new call forfigure(), it will keep updating the output of the first cell, when running in later cells. This is confusing for users. It would make more sense to by default launch a new figure after every cell, unless the axes of the original figure were explicitly used in subsequent calls.