GeezerVison is showing github.com — Retina+jpeg option? · Issue #5 · ipython/matplotlib-inline
DescriptionI 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 Activityminrk commented on 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. Reacted by nspies-celsiustx mgeier commented on Sep 3, 2022 nspies-celsiustx commented on Sep 14, 2022 flying-sheep commented on Jun 28, 2026 Yeah, let’s separate this out. IPython has _retina_shape, so
- that implementation should go to a spot where both matplotlib_inline and IPython can use it
- we should add WebP support to it, it’s not 1995 (i.e. JPEG is not the best format for bandwidth-friendly images on the web anymore)
Where would be a good spot for that code to live? flying-sheep commented on Jun 28, 2026
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|