Enable plotting in same notebook cell which imports matplotlib.pyplot#11916
Merged
minrk merged 2 commits intoipython:masterfrom Nov 5, 2019
Merged
Enable plotting in same notebook cell which imports matplotlib.pyplot#11916minrk merged 2 commits intoipython:masterfrom
minrk merged 2 commits intoipython:masterfrom
Conversation
Contributor
Author
|
I'd also like to add a regression test, but unsure how to programmatically create a separate kernel instance that thinks it supports inline plotting (so that I can test whether the kernel calls |
Member
|
Thanks for the fix! A test like you are describing would probably be best in the ipykernel repo, which has the inline backend and eventloop setup |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes a bug for Jupyter notebooks, that plot commands often do not display until the second time their cell is executed. jupyter/notebook#3691
Specifically, inline display of plots was broken for the first cell which imports
matplotlib.pyplot, but does works in subsequent cells (or even if the same cell is evaluated a second time). This particularly affected libraries that lazily import matplotlib, such as xarray:The problem related to circular imports between matplotlib and ipython. The import of the
matplotlib.pyplotmodule executes a call toIPython.core.pylabtools:activate_matplotlibwhich in turn importsmatplotlib.pyplot(while it is still only partially initialised e.g. thematplotlibmodule may not yet have apyplotattribute). By using a slightly different import syntax it is possible to avoid causing an exception here.Note, there is also a partial work-around in
ipykernel.pylab.backend_inline:_enable_matplotlib_integrationwhich catches the import/attribute error and in that case reschedulesactivate_matplotlibuntil after the cell finishes executing (i.e. after import completes). The problem was that it was late to scheduleflush_figures(as the event is already triggering before the callback gets registered), which is why plotting only worked in subsequent cell evaluations.Tested on python 3.6.7.