Skip to main content
 首页 » 编程设计

python之如何让 Basemap 与时间片的 xarray 绘图一起使用

2024年11月24日28cmt

我需要使用 basemap 将 xarray.DataArray 对象绘制到 shapefile 上。

源数据包含几天的数据。

我希望使用 basemap 在 shapefile 上绘制每个数据集。

... ... 
shapefile1="./shp/CFA_DISTRICT_BODY" 
 
# Select 3 days' datasets 
da_criteria_1or0_hourly_rolled_resampled_sel_slice = da_criteria_1or0_hourly_rolled_resampled.sel(time=slice('2017-01-01', '2017-01-03')) 
 
# Draw each day's dataset and set them drawn horizontally 
p = da_criteria_1or0_hourly_rolled_resampled_sel_slice.plot(levels=[0,1,2], x='longitude', y='latitude', col='time', col_wrap=3) 
 
# Draw the shapefile 
map = Basemap(llcrnrlat=-39.2,urcrnrlat=-33.9,llcrnrlon=140.8,urcrnrlon=150.0,resolution='i') 
map.readshapefile(shapefile1, 'CFA_DISTRICT_BODY', linewidth=0.5) 
 
plt.show() 

上面代码的问题是只有第 3 天的数据集绘制在 shapefile 上。

请您参考如下方法:

您只定义一个Basemap。这将适用于最后一个事件轴。

相反,您将为 FacetGrid 中的每个轴创建一个 basemap 。 这个想法是沿着

grid = data.plot(...) 
 
for ax in grid.axes.flatten(): 
    map = Basemap(..., ax=ax) 
    map.readshapefile(...) 
 
plt.show()