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.

Plot disappears after executing cell second time

See original GitHub issue

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:

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

github_iconTop GitHub Comments

6reactions
tacaswellcommented, Dec 12, 2018

@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.

3reactions
tiagopereiracommented, Jul 19, 2018

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 use qt, the original figure is updated every time I re-run. With widget, the original figure disappears and nothing gets plotted in the second and subsequent runs. It is not logical that a call to plot will 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 the widget backend require this additional step when no other backend does?

This takes me to another point of using the widget backend. I’ve taught a course using jupyter and the widget backend, and the top complaint from students was figures updating in a different cell / forgetting to call plt.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 of inline, and this will create a new figure in the output of every cell that calls plotting functions. This makes sense coming from Mathematica. With widget, however, it creates a figure in the output of the first cell that has plotting calls, but absent a new call for figure(), 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Matplotlib figure window disappears when in interactive mode
If I run this code I see the sine plot for 1 second, then the window disappears. UPDATE 2. I found a solution...
Read more >
Weird problem with plotly plot and DT data table both ...
Weirdly, changing the sliders makes the plot and data table disappear, even though there is no error. Can any help me? Thanks
Read more >
Resolved: Matplotlib figures not showing up or displaying
When I execute the code on my OSX machine in the plotting virtual environment, the histogram is computed and both the grayscale image...
Read more >
FAQ-1025 Why does the embedded graph disappear or show ...
Basically, you can zoom out or pan the worksheet to show the floating graphs embedded in the rightmost gray area of the worksheet....
Read more >
Prism 9: Taking your analyses and graphs to higher dimensions
Note: the figure above shows PCA on two dimensions as a visual example. ... when a graph disappeared from the sheet after performing...
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