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

2023-12-23

我想在第一个轴的右上角添加第二个轴。经过谷歌搜索后,我发现有两种方法可以做到这样的事情:fig.add_axes(), and 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()) 

And inset_axes()不接受projectionarg,所以我无法添加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)?


As inset_axes()不接受projectionarg,迂回的方法是使用InsetPosition()。这样你就可以用通常的方式创建一个轴(使用projection),然后使用“链接”两个轴InsetPosition()。与使用子图或类似图相比,主要优点是插图位置是固定的,您可以调整图形大小或更改主图区域,并且插图将始终位于相对于主轴的相同位置。这是基于这个答案:插入轴的特定位置 https://stackoverflow.com/questions/45378918/specific-location-for-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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐