Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ENH]: Add _repr_svg_ etc to Figure #25846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
oscargus opened this issue May 10, 2023 · 5 comments
Open

[ENH]: Add _repr_svg_ etc to Figure #25846

oscargus opened this issue May 10, 2023 · 5 comments
Labels

Comments

@oscargus
Copy link
Member

oscargus commented May 10, 2023

Problem

Jupyter Qt Console, Spyder and probably Jupyter Notebooks and others can display enriched formats for objects.

It would be convenient if Figure (and possibly other Artists where possible) returned these.

Note that this is different from %matplotlib inline in the sense that the object returns its own representation, not that it plots inline. Although from some perspective it is the same thing. However, I'd like to be able to do something like

fig, ax = plt.subplots()
...
fig

and then fig is shown inline.

And to be even more specific. I have a class where I would like to be able to return different "views" of it in this way. The class already have a _repr_svg_-method (which returns an SVG generated from Matplotlib), but I would like to have properties that returns an object with a different _repr_svg_. One way is to use a simple wrapper, similar to:
https://stackoverflow.com/questions/72613209/displaying-a-matplotlib-figure-from-a-custom-class-in-jupyter-notebook (although a bit more code is required), but maybe we would like to support this directly?

Related to #16782

Proposed solution

Add _repr_svg_, _repr_png_, and maybe some more methods (possibly _repr_mimebundle_). The main question is when these are called and if it brings any performance penalties for people not relying on this?

Something like this will do:

    def _repr_svg_(self):
        buffer = io.StringIO()
        self.savefig(buffer, format="svg")
        return buffer.getvalue()

but this should be discussed before any PR is created.

There are a few methods like this already. Colors can draw themselves and I now see that there is a _repr_html_ for Figure. As well as something for a font entry.

@oscargus
Copy link
Member Author

oscargus commented May 10, 2023

Hmm, it seems like this works (as in Figures show up inline when returned)... Although it returns a html representation. So possibly we may consider adding other formats?

A bit information about the possible _repr_*_:
https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display

@vadimkantorov
Copy link

vadimkantorov commented Sep 19, 2023

_repr_svg_ would also be nice for generating quick manual HTML visualizations which would include/inline multiple SVG strings. For the same end, having a simple shortcut for returning encoded JPEG bytes or data-uri would be nice (then we would need to turn it into data-uri by base64-encoding it)

@flying-sheep

This comment was marked as off-topic.

@jklymak
Copy link
Member

jklymak commented Jan 9, 2025

As noted above, Figure._repr_html_ has existed for >12 years... If folks want to add _repr_svg_ etc that is probably fine, except as noted in https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display

In general, all available formatters will be called when an object is displayed, and it is up to the UI to select which to display. A given formatter should not generally change its output based on what other formats are available - that should be handled at a different level, such as the DisplayFormatter, or configuration.

So it seems like adding more of these will almost certainly make the repr slower.

@tacaswell
Copy link
Member

https://github.com/ipython/ipython/blob/eb3f5ff99c4b929112d8ca8e721d7709e0705386/IPython/core/pylabtools.py#L299-L314

Anything we do in this space needs to be coordinated with IPython to not cause conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants