non è possibile utilizzare tightlayout senza ottenere un errore in matplotlib
Nel codice seguente, se rimuovo il commento dalla riga con la chiamata a tightlayout, ottengo un arresto anomalo in matplotlib. Sarò grato per qualsiasi suggerimento in merito. perché sta succedendo.
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
fig= plt.figure(figsize=(12, 7))
ax= fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.stock_img()
# plt.tight_layout()
plt.show()
Ecco il traceback:
Traceback (ultima chiamata più recente): File "test.py", riga 6, in plt.tight_layout () File "C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ cbook \ deprecation.py ", riga 451, nel file wrapper return func (* args, ** kwargs)" C: \ ProgramData \ Anaconda3 \ envs \ py38 \ 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: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ cbook \ deprecation.py", riga 411 , in wrapper return func (* inner_args, ** inner_kwargs) File "C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ figure.py", riga 2613, in tight_layout kwargs = get_tight_layout_figure (File " C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ tight_layout.py ", riga 303, in get_tight_layout_figure kwargs = auto_adjust_subplotpars (fig, renderer, File" C: \ ProgramData \ Anaconda py3 \ envs \ envs lib \ site-packages \ matplotlib \ tight_layout.py ", riga 84, in auto_adjust_subplotpars bb + = [ ax.get_tightbbox (renderer, for_layout_only = True)] File "C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ axes_base.py", riga 4203, in get_tightbbox bbox = a.get_tightbbox (renderer) File "C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ artist.py", riga 286, in get_tightbbox bbox = Bbox.intersection (bbox, clip_path.get_extents ()) File "C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ path.py ", riga 603, in get_extents return Bbox ([xys.min (axis = 0), xys.max (axis = 0)]) File "C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ numpy \ core_methods.py", riga 43, in _amin return umr_minimum (a, axis, None, out, keepdims, initial, where) ValueError: zero -size array al minimo operazione di riduzione che non ha identità
Sto usando Python 3.8.5 con matplotlib 3.3.1 e CartoPy 0.18.0.
Risposte
Avevo esattamente la stessa configurazione e sono stato in grado di riprodurre il tuo errore. Ma dopo aver cercato traceback simili, ho finalmente trovato questo post , dove l'aggiornamento di Matplotlib da 3.3.1 a 3.3.2 ha risolto il problema.
Quindi se corri per la prima volta in un terminale (con pip o conda)
conda upgrade matplotlib
o
pip install matplotlib==3.3.2
Quindi, assicurati che Matplotlib sia stato aggiornato alla 3.3.2 ed esegui lo script:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
fig= plt.figure(figsize=(12, 7))
ax= fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.stock_img()
plt.tight_layout()
plt.show()
che ha prodotto il risultato desiderato lavorando efficacemente tight_layout()sulla mia macchina.