Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upEnable plotting in same notebook cell which imports matplotlib.pyplot #11916
Conversation
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
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 |
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.
benjimin commentedOct 17, 2019
•
edited
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.pyplot
module executes a call toIPython.core.pylabtools:activate_matplotlib
which in turn importsmatplotlib.pyplot
(while it is still only partially initialised e.g. thematplotlib
module may not yet have apyplot
attribute). 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_integration
which catches the import/attribute error and in that case reschedulesactivate_matplotlib
until 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.