question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Weird 'image size is too large' issue in matplotlib 3.4+

See original GitHub issue

Description

Some geographic plots cannot be plotted due to the image size being too large. This happens when a colorbar is plotted with pcolormesh. It works when the image is plotted with contourf.

Steps to reproduce

import numpy as np
import xarray as xr
import proplot as plot

rng = np.random.default_rng()
x = rng.standard_normal((100, 100))

da = xr.DataArray(x,
     dims=['lat', 'lon'],
     coords={'lat': np.linspace(-90, 90, 100),
             'lon': np.linspace(0, 360, 100)})

fig, ax = plot.subplots(proj='eqearth')
ax.format(coast=True)
m = ax.pcolormesh(da)
ax.colorbar(m)

Actual behavior: [What actually happened]

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/miniconda3/envs/basicf4/lib/python3.8/site-packages/IPython/core/formatters.py in __call__(self, obj)
    339                 pass
    340             else:
--> 341                 return printer(obj)
    342             # Finally look for special method names
    343             method = get_real_method(obj, self.print_method)

~/miniconda3/envs/basicf4/lib/python3.8/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
    250         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    251     if 'retina' in formats or 'png2x' in formats:
--> 252         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
    253     if 'jpg' in formats or 'jpeg' in formats:
    254         jpg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'jpg', **kwargs))

~/miniconda3/envs/basicf4/lib/python3.8/site-packages/IPython/core/pylabtools.py in retina_figure(fig, **kwargs)
    140 def retina_figure(fig, **kwargs):
    141     """format a figure as a pixel-doubled (retina) PNG"""
--> 142     pngdata = print_figure(fig, fmt='retina', **kwargs)
    143     # Make sure that retina_figure acts just like print_figure and returns
    144     # None when the figure is empty.

~/miniconda3/envs/basicf4/lib/python3.8/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    132         FigureCanvasBase(fig)
    133 
--> 134     fig.canvas.print_figure(bytes_io, **kw)
    135     data = bytes_io.getvalue()
    136     if fmt == 'svg':

~/miniconda3/envs/basicf4/lib/python3.8/site-packages/proplot/figure.py in _preprocess(self, *args, **kwargs)
    108         with rc_context, fig_context:
    109             fig.auto_layout()
--> 110             result = func(self, *args, **kwargs)
    111         return result
    112 

~/miniconda3/envs/basicf4/lib/python3.8/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
   2253                 # force the figure dpi to 72), so we need to set it again here.
   2254                 with cbook._setattr_cm(self.figure, dpi=dpi):
-> 2255                     result = print_method(
   2256                         filename,
   2257                         facecolor=facecolor,

~/miniconda3/envs/basicf4/lib/python3.8/site-packages/matplotlib/backend_bases.py in wrapper(*args, **kwargs)
   1667             kwargs.pop(arg)
   1668 
-> 1669         return func(*args, **kwargs)
   1670 
   1671     return wrapper

~/miniconda3/envs/basicf4/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, metadata, pil_kwargs, *args)
    506             *metadata*, including the default 'Software' key.
    507         """
--> 508         FigureCanvasAgg.draw(self)
    509         mpl.image.imsave(
    510             filename_or_obj, self.buffer_rgba(), format="png", origin="upper",

~/miniconda3/envs/basicf4/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py in draw(self)
    399     def draw(self):
    400         # docstring inherited
--> 401         self.renderer = self.get_renderer(cleared=True)
    402         # Acquire a lock on the shared font cache.
    403         with RendererAgg.lock, \

~/miniconda3/envs/basicf4/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py in get_renderer(self, cleared)
    415                           and getattr(self, "_lastKey", None) == key)
    416         if not reuse_renderer:
--> 417             self.renderer = RendererAgg(w, h, self.figure.dpi)
    418             self._lastKey = key
    419         elif cleared:

~/miniconda3/envs/basicf4/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py in __init__(self, width, height, dpi)
     89         self.width = width
     90         self.height = height
---> 91         self._renderer = _RendererAgg(int(width), int(height), dpi)
     92         self._filter_renderers = []
     93 

ValueError: Image size of 68279018x33565395 pixels is too large. It must be less than 2^16 in each direction.

When plot.subplots(proj='cyl') is used, there is no error but there is a large space around the figure.

Screen Shot 2021-07-14 at 14 53 54

Proplot version

matplotlib 3.4.2 proplot 0.7.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
kinyatoridecommented, Jul 14, 2021

Thanks! The error disappeared when I downgraded the matplotlib. Don’t overwork yourself 😊

1reaction
lukelbdcommented, Jul 14, 2021

Looks like another weird auto_layout issue. Maybe related to #244?

This appears to be a problem in matplotlib 3.4+ only. For the time being downgradiing to 3.3.4 or earlier should work. Evidently #251 did not fix all the 3.4-specific bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Matplotlib gives ValueError: Image size pixels is too large ...
It seems like your code is trying to place something way outside of the axes, and so trying to increase the figure size...
Read more >
[Bug]: Large file size when using fill_between() #22803 - GitHub
I am trying to save a plot to PDF (or SVG) that uses the fill_between function between two time series, and the resulting...
Read more >
Resolved: Matplotlib figures not showing up or displaying
In this blog post I detailed how to resolve a pesky issue where matplotlib figures are not displayed to your screen. Symptoms of...
Read more >
Constrained Layout Guide — Matplotlib 3.6.2 documentation
It can happen that your axis labels or titles (or sometimes even ticklabels) go outside the figure area, and are thus clipped. To...
Read more >
Image Filtering - OpenCV
Dilates an image by using a specific structuring element. ... Filter size: Large filters (d > 5) are very slow, so it is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found