0

I have several notebooks open in jupyter lab. Just to simplify the number of files open, I occasionally shut down kernels, especially after saving a file. When I reopen the file, the figures from the last generated output are gone. I have to rerun the notebook.

Further investigation reveals these notebooks have %matplotlib widget in them

How to fix this?

wander95
  • 1,298
  • 1
  • 15
  • 22
  • By choosing `%matplotlib widget` you've opted into the interactive viewer mode that requires an active kernel. So if you shutdown the kernel, there's nothing to display. Remove `%matplotlib widget`. The plot in the final form should show up and continue to display in 'static' mode or until you re-run the cell. If still want at least an image while allowing the interactive mode, you can add in saving the image in your plot code block with [.savefig()](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html) and then display that image in the next cell. ... – Wayne Feb 27 '23 at 18:55
  • For displaying the image in the code cell so it will be encoded in base64 inside the notebook, see [here](https://stackoverflow.com/a/11855133/8508004). (It is also possible to drag and drop it in in a markdown cell so that it becomes an attachement within the saved `ipynb` file.) That way when you shutdown the kernel, the image will continue to display. – Wayne Feb 27 '23 at 18:58
  • Changing `%matplotlib widget` to `%matplotlib notebook` will make the figures interactive until you stop the kernel, then they will become static. The benefit of this method is that the figures will be saved with the `ipynb` file, even after closing it (just like you want). – trent Feb 27 '23 at 21:16

0 Answers0