我正在尝试构建一个简单的函数,它接受一个子图实例(matplotlib.axes._subplots.AxesSubplot
),并将其投影转换为另一种投影方式,例如转换为cartopy.crs.CRS
中的一种投影方式。
这个想法大致上是这样的:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
def make_ax_map(ax, projection=ccrs.PlateCarree()):
# set ax projection to the specified projection
...
# other fancy formatting
ax2.coastlines()
...
# Create a grid of plots
fig, (ax1, ax2) = plt.subplots(ncols=2)
# the first subplot remains unchanged
ax1.plot(np.random.rand(10))
# the second one gets another projection
make_ax_map(ax2)
当然,我可以使用fig.add_subplot()
函数:
fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax1.plot(np.random.rand(10))
ax2 = fig.add_subplot(122,projection=ccrs.PlateCarree())
ax2.coastlines()
我想知道是否有一种适当的matplotlib
方法可以在定义完子图轴投影后进行更改。不幸的是,阅读matplotlib API没有帮助。
projection
关键字确定所有子图的类,因此在subplot_kw
中没有办法传递多个投影方式?例如,对于由plt.subplots(ncols=2)
创建的一个子图集,第一列使用projection='polar'
,第二列使用projection=ccrs.PlateCarree()
,是不可行的。 - Denis Sergeevsubplot_kw
中的关键字会传递给每个轴,因此我认为您无法实现所描述的功能。subplots
函数是一个方便的包装器,用于满足基本用例,如果您需要更多内容,可以使用add_subplot
或类似函数编写自己的包装器函数。 - ajdawsonImageGrid
(或AxesGrid
)中的axes_class
关键字实现。在cartopy的画廊中有一个例子。还有一个PR提交给了cartopy(虽然没有被接受),可以用来创建自定义的GeoAxesGrid
类。 - Denis Sergeev