[英]Issue with tight_layout with matplotlib and cartopy
I recently switched to Matplotlib 3.3.1 and my old script start to complain.我最近切换到 Matplotlib 3.3.1 并且我的旧脚本开始抱怨。 I guess it is an issue with cartopy.
我想这是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()
文件“”,第 4 行,在 plt.tight_layout() 中
File "C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\cbook\\deprecation.py", line 451, in wrapper return func(*args, **kwargs)
文件“C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\cbook\\deprecation.py”,第 451 行,包装器返回 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)
文件“C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\pyplot.py”,第 1490 行,紧缩布局 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)
文件“C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\cbook\\deprecation.py”,第 411 行,包装器返回 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(
文件“C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\figure.py”,第 2613 行,紧缩布局 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,
文件“C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\tight_layout.py”,第 303 行,在 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)]
文件“C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\tight_layout.py”,第 84 行,在 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)
文件“C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\axes_base.py”,第 4203 行,在 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)
文件“C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\artist.py”,第 278 行,在 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())
文件“C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\patches.py”,第 598 行,在 get_window_extent 中返回 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)])
文件“C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\matplotlib\\path.py”,第 603 行,在 get_extents 中返回 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)
文件“C:\\Users\\Vinod\\anaconda3\\lib\\site-packages\\numpy\\core_methods.py”,第 43 行,在 _amin 中 return umr_minimum(a, axis, None, out, keepdims, initial, where)
ValueError: zero-size array to reduction operation minimum which has no identity
ValueError:零大小数组到没有标识的最小化操作
This was a known issue and was addressed in: https://github.com/SciTools/cartopy/issues/1207 .这是一个已知问题,已在: https : //github.com/SciTools/cartopy/issues/1207 中得到解决。 (Making sure you have the latest version of cartopy may resolve this issue).
(确保您拥有最新版本的 cartopy 可能会解决此问题)。
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()
.同时,作为一种解决方法,您可以在调用
fig.canvas.draw()
之前调用plt.tight_layout()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.