如何在 matplotlib 和 cartopy 中轻松添加具有适当位置和大小的 sub_axes?

gep*_*cel 3 python matplotlib cartography cartopy scitools

我想在第一个轴的右上角添加第二个轴。谷歌搜索后,我找到了两种方法来做这样的事情:fig.add_axes(), 和mpl_toolkits.axes_grid.inset_locator.inset_axes. 但fig.add_axes()不接受transformarg。所以下面的代码会抛出一个错误。所以位置不能在父轴坐标下,而是在图形坐标下。

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()) 
Run Code Online (Sandbox Code Playgroud)

并且inset_axes()不接受projectionarg,所以我不能添加ax2为 cartopy geo-axes。

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())
Run Code Online (Sandbox Code Playgroud)

我在matplotlib issue 上问过这个问题。只要它不是 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)))
Run Code Online (Sandbox Code Playgroud)

题:

如何在 matplotlib 和 cartopy 中轻松添加具有适当位置和大小的 sub_axes?

据我了解,在 之后ax.set_extend(),轴的大小会发生变化。那么也许有一种方法可以将 sub_axes 的某个点(例如:的右上角ax2)锚定在 parent_axes 的一个固定位置(例如:的右上角ax1)?

Mar*_*oni 5

由于inset_axes()不接受projectionarg,迂回的方式是使用InsetPosition(). 通过这种方式,您可以以通常的方式(使用projection)创建一个轴,然后使用InsetPosition(). 使用子图或类似的主要优点是插入位置是固定的,您可以调整图形大小或更改主绘图区域,并且插入将始终位于相对于主轴的相同位置。这是基于这个答案:特定位置 inset axes,只是添加了 cartopy 做事方式。

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from mpl_toolkits.axes_grid1.inset_locator import InsetPosition
from shapely.geometry.polygon import LinearRing

extent = [-60, -30, -40, -10]
lonmin, lonmax, latmin, latmax = extent

fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.set_extent(extent, crs=ccrs.PlateCarree())
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE)

# inset location relative to main plot (ax) in normalized units
inset_x = 1
inset_y = 1
inset_size = 0.2

ax2 = plt.axes([0, 0, 1, 1], projection=ccrs.Orthographic(
    central_latitude=(latmin + latmax) / 2,
    central_longitude=(lonmin + lonmax) / 2))
ax2.set_global()
ax2.add_feature(cfeature.LAND)
ax2.add_feature(cfeature.OCEAN)
ax2.add_feature(cfeature.COASTLINE)

ip = InsetPosition(ax, [inset_x - inset_size / 2,
                        inset_y - inset_size / 2,
                        inset_size,
                        inset_size])
ax2.set_axes_locator(ip)

nvert = 100
lons = np.r_[np.linspace(lonmin, lonmin, nvert),
             np.linspace(lonmin, lonmax, nvert),
             np.linspace(lonmax, lonmax, nvert)].tolist()
lats = np.r_[np.linspace(latmin, latmax, nvert),
             np.linspace(latmax, latmax, nvert),
             np.linspace(latmax, latmin, nvert)].tolist()

ring = LinearRing(list(zip(lons, lats)))
ax2.add_geometries([ring], ccrs.PlateCarree(),
                   facecolor='none', edgecolor='red', linewidth=0.75)
Run Code Online (Sandbox Code Playgroud)

插图示例