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

如何将Shapefile叠加到xarray FacetGrid的所有子图中?

问题

我想把Shapefile图层叠加到xarray分面图(FacetGrid)的所有13张子图上,但目前Shapefile和pft数据是分开的两个独立图。以下是我的代码:

pft = xarray.open_dataset("/home/clara/rahu_data/netcdf/jules_frac_9pft_2015_ESA_rahu.nc")
pft['frac'].loc[13,:,:]
pft = pft['frac']

f, ax = plt.subplots(figsize=(10,5))
pft.plot(x="lon", y="lat", col="type", col_wrap=5)
shpfile.plot(ax=ax, facecolor="none")
解决方案

你手动创建的ax和xarray分面图生成的子图不是同一个对象,所以Shapefile只画在了单独的轴上。正确做法是获取xarray返回的FacetGrid对象,遍历它的所有子图轴来叠加Shapefile:

pft = xarray.open_dataset("/home/clara/rahu_data/netcdf/jules_frac_9pft_2015_ESA_rahu.nc")
pft = pft['frac']

# 绘制分面图并保存FacetGrid实例
g = pft.plot(x="lon", y="lat", col="type", col_wrap=5, figsize=(10,5))

# 遍历所有子图轴,逐个添加Shapefile
for ax in g.axes.flat:
    shpfile.plot(ax=ax, facecolor="none")

plt.tight_layout()
plt.show()

这样Shapefile就会显示在每一张分面子图上了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 18:33:15