Skip to content
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

Retina+jpeg option? #5

Open
nspies-celsiustx opened this issue Jul 20, 2021 · 3 comments
Open

Retina+jpeg option? #5

nspies-celsiustx opened this issue Jul 20, 2021 · 3 comments

Comments

@nspies-celsiustx
Copy link

I often work with complex matplotlib figures with millions of points, which I've been displaying in my notebooks using the ipython magic %config InlineBackend.figure_format = 'retina'. Unfortunately, these inline images are quite large in terms of file size.

I'm looking for general suggestions on how to reduce the size of my ipynb files, which can grow into the 10s or even hundreds of MB in size and become a bit unwieldy.

One thought I had was to switch to generating jpegs, but while it looks like I can meaningfully reduce the file size by switching the backend to jpeg and increasing the dpi to 2x:

from matplotlib import rcParams
%config InlineBackend.figure_format = 'jpeg'
rcParams['figure.dpi'] = 200

From this repo, it looks like I can dial in jpeg compression values, which should help reduce file size. However, I can't figure out how to size the jpegs appropriately for a retina display (they're twice as large as they should be, and slightly blurry). Is there a way to display jpegs in jupyter notebooks at similar resolution and size to the defaults provided by 'retina' mode?

See also:
Matplotlib issue
Stackoverflow question

@minrk
Copy link
Member

minrk commented Sep 29, 2021

This isn't an expressed option. It certainly could be, but we never added it because jpg compression isn't a great fit for the sharp lines and text on most matplotlib charts.

You can make 2x figures by adding:

def retina_jpeg(figure):
    """Renderer for 2x jpeg images"""
    jpeg_bytes = print_figure(figure, fmt="jpg")
    w, h = _jpegxy(jpeg_bytes)
    return (jpeg_bytes, {"width": w // 2, "height": h // 2})


def enable_retina_jpeg():
    """Tell IPython to use the 2x jpeg renderer"""
    display_formatter = get_ipython().display_formatter
    # disable any existing Figure formatters
    [f.pop(Figure, None) for f in display_formatter.formatters.values()]
    # register 2x jpg
    jpg_formatter = display_formatter.formatters["image/jpeg"]
    jpg_formatter.for_type(Figure, retina_jpeg)

Since _jpegxy is a private method, you can copy the implementation to be extra safe, but I don't think it's going anywhere.

Here's a notebook showing a complete example.

But I think the 'right' way to do it here is to separate the configuration from a single 'retina' vs 'png' or 'jpg' option to a separate 'scale' option. Where e.g. a figure dpi and screen dpi are separate values, and we return the metadata with the appropriate scale.

@mgeier
Copy link

mgeier commented Sep 3, 2022

@nspies-celsiustx Did you try using SVG with rasterization?

Theoretically, this would give you sharp edges for lines and text etc., but still a reduced size.

See https://matplotlib.org/stable/gallery/misc/rasterization_demo.html

@nspies-celsiustx
Copy link
Author

@nspies-celsiustx Did you try using SVG with rasterization?

Theoretically, this would give you sharp edges for lines and text etc., but still a reduced size.

See https://matplotlib.org/stable/gallery/misc/rasterization_demo.html

Thanks for the suggestion @mgeier - this sounds like a good option for one-offs but I'm looking for a solution that works for every graphic generated without additional boilerplate.

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

No branches or pull requests

3 participants