You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Remove the necessity to run %matplotlib inline in Jupyter Notebooks by running configure_inline_support #12190
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 commentedon Mar 14, 2020
I would try to update your matplotlib/ipykernel/ipython, it should be automatic since a couple of releases ago:
tdpetrou commentedon Mar 14, 2020
Importing
matplotlib.pyplottriggers the importipykernel.pylabl.backend_inline. This runs the function_enable_matplotlib_integration, which imports fromIPython.core.pylabtoolsand callsactivate_matplotlibandconfigure_inline_support. Try running just the second cell without importing pyplot.tdpetrou commentedon Mar 14, 2020
Here's a summary of what happens
What happens when jupyter notebook is launched from command line
ipykernel.kernelapp.IPKernelAppis created and theinitializemethod callsinit_gui_pylabwhich sets the backend to inlineWhat happens when
import matplotlib.pyplotis runswitch_backendis calledimportlib.import_module(backend_name)is runipykernel.pylab.backend_inlineis imported which runs_enable_matplotlib_integrationIPython.core.pylabtoolsand callsactivate_matplotlibandconfigure_inline_supportswitch_backendagain, but runningimportlib.import_module(backend_name)doesn't trigger calling of module since its already imported.configure_inline_supportregistersflush_figuresfromipykernel.pylab.backend_inlinefor post_execute eventselect_figure_formatsis run once to register function hook for each type of imagepng_formatter = shell.display_formatter.formatters['image/png']png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))print_figurefunction 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.install_repl_displayhookis called and registers event post_execute to draw_all figures - draw using the canvasWhat happens when
%matplotlib inlineis runmatplotlibis run fromIPython/core/magics/pylab.pyshell.enable_matplotlib(gui)fromIPython/core/interactiveshell.pyfrom IPython.core import pylabtools as ptand callspt.find_gui_and_backend- imports matplotlibpt.activate_matplotlib(backend)- backend set, imports pyplotipython.core.pylabtools.activate_matplotlibagainpt.configure_inline_support(self, backend)does same as abovetacaswell commentedon 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__toFigurein Matplotlib that then defers to a method on the canvas object. We would then put the correct methods on the canvas classes frominline(the static backend),nbagg(which we will hopefully be able to retire in the next year or two), andipympl(the ipywidegts based notebook backend).tdpetrou commentedon 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 commentedon 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 commentedon Apr 14, 2021
@timics In recent versions of jupyter notebook I have been omitting
%matplotlib inlineentirely and it still works, so I am assuming ithas been updated to remove the need for this call.westurner commentedon Apr 15, 2021
%matplotlib inlineclarify?jklymak commentedon 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?