简体   繁体   中英

Issue with tight_layout with matplotlib and cartopy

I recently switched to Matplotlib 3.3.1 and my old script start to complain. I guess it is an issue with cartopy. Here is a minimum reproducible example

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()

Any suggestion to fix this issue?

Here I copy the error message:

Traceback (most recent call last):

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

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

File "C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\pyplot.py", line 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", line 411, in wrapper return func(*inner_args, **inner_kwargs)

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

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

File "C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\tight_layout.py", line 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", line 4203, in get_tightbbox bbox = a.get_tightbbox(renderer)

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

File "C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\patches.py", line 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", line 603, in get_extents return Bbox([xys.min(axis=0), xys.max(axis=0)])

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

ValueError: zero-size array to reduction operation minimum which has no identity

This was a known issue and was addressed in: https://github.com/SciTools/cartopy/issues/1207 . (Making sure you have the latest version of cartopy may resolve this issue).

In the meanwhile, as a workaround it was noted that you could make a call to fig.canvas.draw() before your call to plt.tight_layout() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM