使用gridspec并排打印多个Cartopy贴图?

2024-03-28 12:47:16 发布

您现在位置:Python中文网/ 问答频道 /正文

目标是生成一个分割的图形,左侧有一个大型正交地图,右侧有十二个较小的EqualEarth投影。最终将设置动画,填充数据等。目前,最小的示例如下:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

fig = plt.figure(figsize=(16,8), constrained_layout=True)
gs = fig.add_gridspec(1, 2)

ax0 = fig.add_subplot(gs[0,0])
ax0 = plt.axes(projection=ccrs.Orthographic())
ax0.coastlines(resolution='110m')

nrows = 4
ncols = 3

for i in range(nrows*ncols):
    ax1 = fig.add_subplot(nrows, ncols, i+1, projection=ccrs.EqualEarth())
    ax1.set_global()
    ax1.coastlines()

这将产生以下结果:

Actual Result

首选结果如下所示:

Preferred Result

我如何做到这一点?如果有更好的方法,它不必使用gridspec

编辑:使用以下代码成功:

fig = plt.figure(figsize=(12, 5.5), constrained_layout=True)
gs0 = fig.add_gridspec(1, 2, width_ratios=[1, 2])

ax0 = fig.add_subplot(gs0[0], projection=ccrs.Orthographic())
ax0.coastlines()

gs01 = gs0[1].subgridspec(4, 3)

for a in range(4):
    for b in range(3):
        ax = fig.add_subplot(gs01[a, b], projection=ccrs.EqualEarth())

Tags: inaddforfigrangepltprojectionncols
1条回答
网友
1楼 · 发布于 2024-03-28 12:47:16

您基本上希望执行此处描述的操作: https://matplotlib.org/tutorials/intermediate/gridspec.html#gridspec-using-subplotspec

我没有费心去下载cartopy,但你明白了


fig = plt.figure(constrained_layout=True)
gs0 = fig.add_gridspec(1, 2, width_ratios=[1, 2])

ax0 = fig.add_subplot(gs0[0])

gs01 = gs0[1].subgridspec(4, 3)

for a in range(4):
    for b in range(3):
       ax = fig.add_subplot(gs01[a, b])

enter image description here

相关问题 更多 >