Skip to content

Add _repr_html_ for inline backend to eliminate need for %matplotlib inline #16782

Closed as not planned
@tdpetrou

Description

@tdpetrou

When launching a jupyter notebook, the environment variable MPLBACKEND is set to inline, but figures will still not be displayed in the output unless %matplotlib inline or import matplotlib.pyplot is run. Both of these commands hook IPython to a function that gets triggered whenever a Figure is the output from a cell. This function is responsible for printing the figure to the screen.

Screen Shot 2020-03-15 at 9 32 10 PM

Proposed solution:

A _repr_html_ method exists for figures, but is only implemented for webagg backends. Something like the following could be added to _repr_html_.

if rcParams['backend'] == 'module://ipykernel.pylab.backend_inline':
    from io import BytesIO
    from base64 import b64encode
    png_bytes = BytesIO()
    self.canvas.print_figure(png_bytes, format='png')
    s = png_bytes.getvalue()
    s1 = b64encode(s).decode()
    return f'<img src="data:image/png;base64, {s1}"/>'

This is @tacaswell's idea. I previously created an issue with IPython - ipython/ipython#12190 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: inactiveMarked by the “Stale” Github Action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions