Problema con tight_layout con matplotlib e cartopy

Oct 14 2020

Di recente sono passato a Matplotlib 3.3.1 e il mio vecchio script inizia a lamentarsi. Immagino sia un problema con la cartopia. Ecco un esempio minimo riproducibile

import cartopy.crs as ccrs    
fig, ax = plt.subplots(2, 2,
                       subplot_kw=dict(projection=ccrs.PlateCarree()),
                      figsize=[12,7], sharex=True, sharey=True)
plt.tight_layout()

Qualche suggerimento per risolvere questo problema?

Qui copio il messaggio di errore:

Traceback (la chiamata più recente per ultima):

File "", riga 4, in plt.tight_layout ()

File "C: \ Users \ Vinod \ anaconda3 \ lib \ site-packages \ matplotlib \ cbook \ deprecation.py", riga 451, in wrapper return func (* args, ** kwargs)

File "C: \ Users \ Vinod \ anaconda3 \ lib \ site-packages \ matplotlib \ pyplot.py", riga 1490, in tight_layout gcf (). Tight_layout (pad = pad, h_pad = h_pad, w_pad = w_pad, rect = rect )

File "C: \ Users \ Vinod \ anaconda3 \ lib \ site-packages \ matplotlib \ cbook \ deprecation.py", riga 411, in wrapper return func (* inner_args, ** inner_kwargs)

File "C: \ Users \ Vinod \ anaconda3 \ lib \ site-packages \ matplotlib \ figure.py", riga 2613, in tight_layout kwargs = get_tight_layout_figure (

File "C: \ Users \ Vinod \ anaconda3 \ lib \ site-packages \ matplotlib \ tight_layout.py", riga 303, in get_tight_layout_figure kwargs = auto_adjust_subplotpars (fig, renderer,

File "C: \ Users \ Vinod \ anaconda3 \ lib \ site-packages \ matplotlib \ tight_layout.py", riga 84, in auto_adjust_subplotpars bb + = [ax.get_tightbbox (renderer, for_layout_only = True)]

File "C: \ Users \ Vinod \ anaconda3 \ lib \ site-packages \ matplotlib \ axes_base.py", riga 4203, in get_tightbbox bbox = a.get_tightbbox (renderer)

File "C: \ Users \ Vinod \ anaconda3 \ lib \ site-packages \ matplotlib \ artist.py", riga 278, in get_tightbbox bbox = self.get_window_extent (renderer)

File "C: \ Users \ Vinod \ anaconda3 \ lib \ site-packages \ matplotlib \ patches.py", riga 598, in get_window_extent return self.get_path (). Get_extents (self.get_transform ())

File "C: \ Users \ Vinod \ anaconda3 \ lib \ site-packages \ matplotlib \ path.py", riga 603, in get_extents restituisce Bbox ([xys.min (axis = 0), xys.max (axis = 0) ])

File "C: \ Users \ Vinod \ anaconda3 \ lib \ site-packages \ numpy \ core_methods.py", riga 43, in _amin return umr_minimum (a, axis, None, out, keepdims, initial, where)

ValueError: array di dimensioni zero per l'operazione di riduzione minima che non ha identità

Risposte

1 jwalton Oct 14 2020 at 21:07

Questo era un problema noto ed è stato risolto in: https://github.com/SciTools/cartopy/issues/1207. (Assicurarsi di disporre dell'ultima versione di cartopy potrebbe risolvere questo problema).

Nel frattempo, come soluzione alternativa, è stato notato che potresti chiamare a fig.canvas.draw()prima di chiamare a plt.tight_layout().