Currently, whenever a Jupyter Notebook is launched, the MPLBACKEND environment variable is set to module://ipykernel.pylab.backend_inline in init_gui from ipykernel.kernelapp.
While this sets matplotlib's backend to inline, figures not created via pyplot will not be shown in the notebook output.
This is because the function configure_inline_support from python.core.pylabtools is never run. It is responsible for hooking a function that gets triggered whenever a matplotlib figure is the output of a cell. The function calls the figure's canvas's print_figure function and returns the raw bytes of the image.
This function configure_inline_support does get run when matplotlib.pyplot is imported and when %matplotlib inline is run.
Having to do either of these things can be eliminated by running configure_inline_support within init_gui after the shell is retrieved. I think the following could be enough:
Importing matplotlib.pyplot triggers the import ipykernel.pylabl.backend_inline. This runs the function _enable_matplotlib_integration, which imports from IPython.core.pylabtools and calls activate_matplotlib and configure_inline_support. Try running just the second cell without importing pyplot.
What happens when jupyter notebook is launched from command line
When starting a new notebook, ipykernel.kernelapp.IPKernelApp is created and the initialize method calls init_gui_pylab which sets the backend to inline
What happens when import matplotlib.pyplot is run
switch_backend is called
importlib.import_module(backend_name) is run
ipykernel.pylab.backend_inline is imported which runs _enable_matplotlib_integration
which imports from IPython.core.pylabtools and calls activate_matplotlib and configure_inline_support
activate_matplotlib callsswitch_backend again, but running importlib.import_module(backend_name) doesn't trigger calling of module since its already imported.
configure_inline_support registers flush_figures from ipykernel.pylab.backend_inline for post_execute event
select_figure_formats is run once to register function hook for each type of image
print_figure function is finally called which calls the fig.canvas.print_figure function to place data in a BytesIO stream. It is this data that is returned.
In pyplot, install_repl_displayhook is called and registers event post_execute to draw_all figures - draw using the canvas
What happens when %matplotlib inline is run
Method matplotlib is run from IPython/core/magics/pylab.py
Calls shell.enable_matplotlib(gui) from IPython/core/interactiveshell.py
This runs from IPython.core import pylabtools as pt and calls
You do not want to import Matplotlib on notebook start up because if the user does not need it we should not waste the import time.
We also need to be sure that these changes do not break either nbagg or ipympl.
Steps 7-9 are the key steps for making the implicit display work.
Probably the right way to fix this is to add a __repr_html__ to Figure in Matplotlib that then defers to a method on the canvas object. We would then put the correct methods on the canvas classes from inline (the static backend), nbagg (which we will hopefully be able to retire in the next year or two), and ipympl (the ipywidegts based notebook backend).
Came here from the 'matplotlib rcParams are not picked up from if "%matplolib inline" is in the same cell' bug, curious if anyone is still looking into either fixing that issue or getting rid of the need for "%matplotlib inline"
@timics In recent versions of jupyter notebook I have been omitting %matplotlib inline entirely and it still works, so I am assuming ithas been updated to remove the need for this call.
Activity
Carreau commented on Mar 14, 2020
I would try to update your matplotlib/ipykernel/ipython, it should be automatic since a couple of releases ago:
tdpetrou commented on Mar 14, 2020
Importing matplotlib.pyplot triggers the import ipykernel.pylabl.backend_inline. This runs the function _enable_matplotlib_integration, which imports from IPython.core.pylabtools and calls activate_matplotlib and configure_inline_support. Try running just the second cell without importing pyplot.
tdpetrou commented on Mar 14, 2020
Here's a summary of what happens
What happens when jupyter notebook is launched from command line
What happens when import matplotlib.pyplot is run
What happens when %matplotlib inline is run
tacaswell commented on Mar 15, 2020
You do not want to import Matplotlib on notebook start up because if the user does not need it we should not waste the import time.
We also need to be sure that these changes do not break either nbagg or ipympl.
Steps 7-9 are the key steps for making the implicit display work.
Probably the right way to fix this is to add a __repr_html__ to Figure in Matplotlib that then defers to a method on the canvas object. We would then put the correct methods on the canvas classes from inline (the static backend), nbagg (which we will hopefully be able to retire in the next year or two), and ipympl (the ipywidegts based notebook backend).
tdpetrou commented on Mar 15, 2020
@tacaswell Agreed, _repr_html_ would be a nice way to fix this to avoid the extra imports at the start and not affect the implicit plotting.
timics commented on Mar 29, 2021
Came here from the 'matplotlib rcParams are not picked up from if "%matplolib inline" is in the same cell' bug, curious if anyone is still looking into either fixing that issue or getting rid of the need for "%matplotlib inline"
AstroMike commented on Apr 14, 2021
@timics In recent versions of jupyter notebook I have been omitting %matplotlib inline entirely and it still works, so I am assuming ithas been updated to remove the need for this call.
westurner commented on Apr 15, 2021
jklymak commented on Apr 3, 2025
Matplotlib has figure.Figure._repr_html_.
https://github.com/matplotlib/matplotlib/blob/a364dc5295e332b2d7e038f18104d0a94aad7ef2/lib/matplotlib/figure.py#L2743-L2748
does it not work here?