原来的代码:

# Loop through years and create subplots
for i, tif_file in enumerate(tif_files):
    ax = fig.add_subplot(gs[i // 6, i % 6], projection=ccrs.PlateCarree())
    ax.set_title(f"{i + 2000}")

    # Read NDVI data
    with rasterio.open(tif_file) as src:
        ndvi_data = src.read(1, masked=True)
        extent = [src.bounds.left, src.bounds.right, src.bounds.bottom, src.bounds.top]

    # 数组无效值掩膜
    data_mask = np.ma.array(ndvi_data, mask=nodata_mask)

    # Plot NDVI data
    im = ax.imshow(data_mask, cmap=cmap, extent=extent, origin='upper', transform=ccrs.PlateCarree())

    # Add province boundaries
    ax.add_geometries(province_geometries, ccrs.PlateCarree(), facecolor='none', edgecolor='black', linewidth=0.5)

但是出图只有第一个子图有矢量,其他子图没有,修改最后一句,则每张子图都能显示矢量了。

 # Add province boundaries to the subplot
    ax.add_geometries(Reader(province_shp).geometries(), ccrs.PlateCarree(),facecolor='none', edgecolor='black', linewidth=0.3)

province_shp为完成路径的shp文件。

11-15 16:46