我想在第一个轴的右上角添加第二个轴。经过谷歌搜索后,我发现有两种方法可以做到这样的事情:fig.add_axes()
, and mpl_toolkits.axes_grid.inset_locator.inset_axes
。但是fig.add_axes()
不接受transform
arg。所以下面的代码会抛出错误。所以位置不能在父轴坐标下,而是在图形坐标下。
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
fig, ax = plt.subplots(1, 1, subplot_kw={'projection': ccrs.PlateCarree()})
ax2 = fig.add_axes([0.8, 0, 0.2, 0.2], transform=ax.transAxes, projection=ccrs.PlateCarree())
And inset_axes()
不接受projection
arg,所以我无法添加ax2
作为 cartopy 地理轴。
from mpl_toolkits.axes_grid.inset_locator import inset_axes
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
fig, ax = plt.subplots(1, 1, subplot_kw={'projection': ccrs.PlateCarree()})
# The following line doesn't work
ax2 = inset_axes(ax, width='20%', height='20%', axes_kwargs={'projection': ccrs.PlateCarree()})
# Doesn't work neither:
ax2 = inset_axes(ax, width='20%', height='20%', projection=ccrs.PlateCarree())
我已经在以下位置提出了问题matplotlib 问题 https://github.com/matplotlib/matplotlib/issues/8986#issuecomment-320476304。看起来下面的代码只要不是 cartopy 轴就可以很好地工作。
import matplotlib as mpl
fig, ax = plt.subplots(1, 1)
box = mpl.transforms.Bbox.from_bounds(0.8, 0.8, 0.2, 0.2)
ax2 = fig.add_axes(fig.transFigure.inverted().transform_bbox(ax.transAxes.transform_bbox(box)))
问题:
如何在 matplotlib 和 cartopy 中轻松添加具有适当位置和大小的 sub_axes ?
据我了解,之后ax.set_extend()
,轴的大小将会改变。所以也许有一种方法可以使 sub_axes 的某个点(例如:右上角ax2
)可以锚定在parent_axes的一个固定位置(例如:右上角ax1
)?