You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NaturalEarth V5_0_0迁移后Cartopy代码报错及地图适配问题

问题:NaturalEarth V5.0.0迁移后Cartopy绘图报错及区域适配异常

一、迁移后的初始报错

将代码迁移至NaturalEarth V5_0_0版本后,原有Cartopy绘图代码出现报错:

报错详情

File "/home/ludo915/Eco-Tech H2GAM/maps.py", line 242, in compute_maps
a.add_geometries(countryEU['F'].polygon, ccrs.PlateCarree(),
TypeError: 'NaturalEarth_v5_0_0' object is not subscriptable

此前可正常运行的代码片段

fig = plt.figure(figsize=(8,8))
gs = fig.add_gridspec(1, 1)
ax1 = fig.add_subplot(gs[0, 0], projection=ccrs.PlateCarree())
axes = [ax1]
ax1.background_patch.set_fill(False)
for a in axes:
    a.add_geometries(countryEU['F'].polygon, ccrs.PlateCarree(),
    edgecolor=grayDark, lw=2, facecolor=grayDark, alpha=0.6, zorder=0)
    a.set_extent([-5,10,41,52])
    a.set_aspect('auto')
    a.outline_patch.set_linewidth(0.)
    pass

该问题已在GitHub及Stack Overflow求助,之后改用regionmask调用法国区域,代码可运行,但出现新问题:法国区域的灰色前景无法与地图正确适配。

二、修改后的适配异常问题

修改后的代码片段

fig = plt.figure(figsize=(8,8))
gs = fig.add_gridspec(1, 1)
ax1 = fig.add_subplot(gs[0, 0], projection=ccrs.PlateCarree())
axes = [ax1]
ax1.background_patch.set_fill(False)
for a in axes:
    a.add_geometries([regionmask.defined_regions.prudence["FR"].polygon,], ccrs.PlateCarree(),
    edgecolor=grayDark, lw=2, facecolor=grayDark, alpha=0.6, zorder=0)
    a.set_extent([-5,10,41,52])
    a.set_aspect('auto')
    a.outline_patch.set_linewidth(0.)
    pass

cax = ax1.scatter(risk1Maps[counter].lon,risk1Maps[counter].lat,c=risk1Maps[counter].idx,
cmap='RdYlGn_r', s=markersize*5, vmin=0, vmax=risk1max, zorder=4)
cbar = fig.colorbar(cax, orientation='horizontal', pad=0, aspect=50,
fraction=.01, extend='max', drawedges=False, ticks=[0, risk1max])
cbar.ax.set_xticklabels(['low', 'high'])
cbar.ax.xaxis.set_ticks_position('top')
cbar.ax.xaxis.set_label_position('top')

ax1.text(0,.0,'Data\nCAMS\ndata.gouv.fr', transform=ax1.transAxes,fontdict={'size':12})

currentDateWD = today.strftime('%a, %d - %m, %Y')
ax1.set_title('PM2.5 concentrations:\n{:}\n'.format(currentDateWD + " + "+ str (counter) + " days"),
loc='left', pad=-60)

fig.subplots_adjust(bottom=.01, left=.01, right=.99, top=.99)
#plt.show()
buffer = io.BytesIO()
plt.savefig(buffer, format='png', dpi=70)
buffer.seek(0)
images1.append(imageio.imread(buffer))
buffer.close()
plt.close()

异常效果

SO2 Forecasts


内容的提问来源于stack exchange,提问作者Ludogiraud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 03:01:08