FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Remove the necessity to run %matplotlib inline in Jupyter Notebooks by running configure_inline_support · Issue #12190 · ipython/ipython · GitHub

Repository navigation

Remove the necessity to run %matplotlib inline in Jupyter Notebooks by running configure_inline_support #12190

Open

Description

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:

from IPython.core.pylabtools import configure_inline_support
configure_inline_support(shell, 'module://ipykernel.pylab.backend_inline')

I'd be very excited to have this change and never worry about running %matplotlib inline again.

Activity

  1. Carreau commented on Mar 14, 2020

    Member

    I would try to update your matplotlib/ipykernel/ipython, it should be automatic since a couple of releases ago:

  2. tdpetrou commented on Mar 14, 2020

    Author

    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.

  3. tdpetrou commented on Mar 14, 2020

    Author

    Here's a summary of what happens

    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

    1. switch_backend is called
    2. importlib.import_module(backend_name) is run
    3. ipykernel.pylab.backend_inline is imported which runs _enable_matplotlib_integration
    4. which imports from IPython.core.pylabtools and calls activate_matplotlib and configure_inline_support
    5. activate_matplotlib callsswitch_backend again, but running importlib.import_module(backend_name) doesn't trigger calling of module since its already imported.
    6. configure_inline_support registers flush_figures from ipykernel.pylab.backend_inline for post_execute event
    7. select_figure_formats is run once to register function hook for each type of image
    8. Example: png_formatter = shell.display_formatter.formatters['image/png']
    9. png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    10. 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.
    11. 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

    1. Method matplotlib is run from IPython/core/magics/pylab.py
    2. Calls shell.enable_matplotlib(gui) from IPython/core/interactiveshell.py
    3. This runs from IPython.core import pylabtools as pt and calls
    4. pt.find_gui_and_backend - imports matplotlib
    5. pt.activate_matplotlib(backend) - backend set, imports pyplot
    6. importing pyplot calls ipython.core.pylabtools.activate_matplotlib again
    7. pt.configure_inline_support(self, backend) does same as above
  4. tacaswell commented on Mar 15, 2020

    Contributor

    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).

  5. tdpetrou commented on Mar 15, 2020

    Author

    @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.

  6. 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"

  7. 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.

  8. westurner commented on Apr 15, 2021

    Contributor
    • DOC: what do the docs for %matplotlib inline clarify?
  9. jklymak commented on Apr 3, 2025

    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.

    Matplotlib has figure.Figure._repr_html_.

    https://github.com/matplotlib/matplotlib/blob/a364dc5295e332b2d7e038f18104d0a94aad7ef2/lib/matplotlib/figure.py#L2743-L2748

    does it not work here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL