impossible d'utiliser tightlayout sans obtenir une erreur dans matplotlib
Dans le code suivant, si je décommente la ligne avec l'appel à tightlayout, j'obtiens un plantage dans matplotlib. Je serai reconnaissant pour toute suggestion re. pourquoi cela se produit.
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()
Voici le retraçage:
Traceback (dernier appel en dernier): Fichier "test.py", ligne 6, dans plt.tight_layout () Fichier "C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ cbook \ deprecation.py ", ligne 451, dans le wrapper return func (* args, ** kwargs) Fichier" C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ pyplot.py ", ligne 1490, dans tight_layout gcf ( ) .tight_layout (pad = pad, h_pad = h_pad, w_pad = w_pad, rect = rect) Fichier "C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ cbook \ deprecation.py", ligne 411 , dans le wrapper return func (* inner_args, ** inner_kwargs) File "C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ figure.py", ligne 2613, dans tight_layout kwargs = get_tight_layout_figure (File " C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ tight_layout.py ", ligne 303, dans get_tight_layout_figure kwargs = auto_adjust_subplotpars (fig, renderer, File" C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ tight_layout.py ", ligne 84, dans auto_adjust_subplotpars bb + = [ ax.get_tightbbox (renderer, for_layout_only = True)] Fichier "C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ axes_base.py", ligne 4203, dans get_tightbbox bbox = a.get_tightbbox (rendu) Fichier "C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ artist.py", ligne 286, dans get_tightbbox bbox = Bbox.intersection (bbox, clip_path.get_extents ()) Fichier "C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ matplotlib \ path.py ", ligne 603, dans get_extents return Bbox ([xys.min (axis = 0), xys.max (axis = 0)]) Fichier "C: \ ProgramData \ Anaconda3 \ envs \ py38 \ lib \ site-packages \ numpy \ core_methods.py", ligne 43, dans _amin return umr_minimum (a, axis, None, out, keepdims, initial, where) ValueError: zero -size array au minimum d'opération de réduction qui n'a pas d'identité
J'utilise Python 3.8.5 avec matplotlib 3.3.1 et CartoPy 0.18.0.
Réponses
J'avais exactement la même configuration et j'ai pu reproduire votre erreur. Mais après avoir recherché des traces similaires, j'ai finalement trouvé cet article , où la mise à niveau de Matplotlib de 3.3.1 à 3.3.2 a résolu le problème.
Donc, si vous exécutez d'abord dans un terminal (avec pip ou conda)
conda upgrade matplotlib
ou
pip install matplotlib==3.3.2
Ensuite, assurez-vous que Matplotlib a été mis à jour vers 3.3.2 et exécutez votre 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()
qui a produit le résultat souhaité en travaillant efficacement tight_layout()sur ma machine.